What the Heck is an ENUM?

What the Heck is an ENUM?

  • Comments 6

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

Leave a Comment
  • Please add 6 and 2 and type the answer here:
  • Post
  • 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

  • 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

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

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

  • @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

  • @Stranger:

    thanks for your clarification.

  • 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).

Page 1 of 1 (6 items)