Enum and underlying types

In languages such as C#, enumerations can be given an underlying type, such as Byte or Int64. In PowerShell 5 and PowerShell Core 6.1 and older, the enumeration type is fixed to Int32. This type is shown using the following command:

PS> enum MyEnum {
>> First = 1
>> }
PS> [MyEnum].GetEnumUnderlyingType()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType

Any enumeration value may therefore be cast to its underlying type, or any numeric type capable of supporting the value:

[Int][MyEnum]::First

A new feature has been added to PowerShell Core that will grant you the ability to set the underlying type. This feature is likely to appear in PowerShell Core 6.2 but is not available in the preview versions at the time of writing. The notation that will be allowed is shown as follows:

enum MyEnum : UInt64 {
First = 0
Last = 18446744073709551615
}
..................Content has been hidden....................

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