Defining an enumeration

An enumeration is a set of named constants. The .NET framework is full of examples of enumerations. For example, the System.Security.AccessControl.FileSystemRights enumeration describes all of the numeric values that are used to define access rights for files or directories.

Enumerations are also used in PowerShell itself, for example, System.Management.Automation.ActionPreference contains the values for the preference variables, such as ErrorActionPreference and DebugPreference.

Enumerations are created using the enum keyword, and this is followed by a list of values:

enum MyEnum {
First = 1
Second = 2
Third = 3
}

Each name must be unique within the enumeration, and must start with a letter or an underscore. The name may contain numbers after the first character. The name cannot be quoted and cannot contain the hyphen character.

The value does not have to be unique. One or more names in an enumeration can share a single value:

enum MyEnum {
One = 1
First = 1
Two = 2
Second = 2
}

The style of the preceding enumeration is odd: it defines two sets of names in a single enumeration, which is not a good practice to adopt.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.95.244