Welcome to MSDN Blogs Sign in | Join | Help

What the Heck is an ENUM?

In my latest post Explore your [Environment] I said the following:

PS> [System.Environment+SpecialFolder]

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
False    True     SpecialFolder                            System.Enum

Notice that it is an ENUM.  The great things about ENUMS is that when you specify an invalid value - it tells you what the valid values are:

One of the comments made me realize that a lot of people will have no idea what I'm talking about so I thought I would spend a minute explaining. 

ENUM is short for ENUMERATION.  In .NET, ENUM is a base class for a set of named constants.  You can think of it as a set of strings (that is not entirely accurate but for our purposes - it is a perfect explanation.   So when you see that something takes an ENUM, what it means is that it doesn't just take any old string - it takes one of a set of strings. 

What is that set of string? - That's the beauty of an ENUM, it will tell it.  All you have to do is to provide a wrong value and it will tell you what the right values are.

Enjoy

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Published Sunday, December 14, 2008 4:33 PM by PowerShellTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: What the Heck is an ENUM?

Thanks.

I konw what is ENUM type(just a set of constants).I just wonder why the name is [System.Environment+SpecialFolder] (in PS)but not [System.Environment.SpecialFolder](in C#).

again,look this:

http://blogs.msdn.com/powershell/archive/2007/01/23/how-to-create-enum-in-powershell.aspx

PS > New-Enum my.color blue red yellow

PS > [my.color]::blue

PS > [my+color]::blue

Unable to find type [my+color].....

#==========================

All .net ENUM type need "+" instead of "." in PS

PS C:\test> [ActivationContext.ContextForm]

Unable to find type [ActivationContext.ContextForm]: make sure that the assembl

y containing this type is loaded.

At line:1 char:31

+ [ActivationContext.ContextForm] <<<<

PS C:\test> [ActivationContext+ContextForm]

IsPublic IsSerial Name                                     BaseType

-------- -------- ----                                     --------

False    True     ContextForm                              System.Enum

Monday, December 15, 2008 12:48 AM by applepwc

# re: What the Heck is an ENUM?

There are a bunch of PowerShell script samples up on MSDN that demonstrate some of the many hundreds of ENUMs available inside .NET.

To view samples, take a look at http://msdn.microsoft.com/en-us/library/tags-cloud.aspx?tag=powershell+enum

Monday, December 15, 2008 7:34 AM by tfl

# Getting valid values for a enum

This is probably better than throwing an error to list valid values for an enum:

[system.enum]::getnames([System.Environment+SpecialFolder])

Monday, December 15, 2008 6:19 PM by Ronald S Woan

# re: What the Heck is an ENUM?

@applepwc In short the "+" in [System.Environment+SpecialFolder] distinguishes a namespace from a nested type. [System.Environment] is the typename of the Environment class. System is the namespace, Environment the class name. The +SpecialFolder means that SpecialFolder is a type defined _within_ the Environment class.

Compare this with c#:

namespace System

{

  class Environment

  {

     // enum defined within Environment class!

     enum SpecialFolder {...}

  }

}

// => Type in PS: [System.Environment+SpecialFolder]

Now compare this with a SpecialFolder enum defined in a System.Environment namespace:

namespace System.Environment

{

  // enum defined within System.Environment namespace!

  enum SpecialFolder {...}

}

// => Type in PS: [System.Environment.SpecialFolder]

hth

Wednesday, December 17, 2008 3:52 AM by Stranger

# re: What the Heck is an ENUM?

@Stranger:

thanks for your clarification.

Wednesday, December 17, 2008 8:16 PM by applepwc

# re: What the Heck is an ENUM?

It would be nice if Get-Help were to specifically list enum values.  Or is there a way to reflect upon a cmdlet and find the types of its parameters, and then use the static method on System.Enum to pull the values?  I would rather not have to inject strings into Get-Help contents, but you PowerShell folks haven't seem to have thought of this (or if you have, it is not clear to the public).

Friday, December 19, 2008 3:18 PM by Luke Breuer

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker