Class EnumUtil
Provides utility methods for manipulating enumerations.
public static class EnumUtil
- Inheritance
-
EnumUtil
- Inherited Members
Methods
CreateEnumArrayByValue<T>(T, int)
Creates an array containing elements of the specified enum type T
, of the specified length.
If the length is greater than the number of enum values,
the remaining elements are filled with a default value, otherwise array are truncated.
public static T[] CreateEnumArrayByValue<T>(T defaultValue, int length) where T : Enum
Parameters
defaultValue
TThe default value that will fill the elements of an array if its size is greater than the number of enum values.
length
intThe length of result array.
Returns
- T[]
The created array.
Type Parameters
T
The specified enum type.
Remarks
The elements of the array are sorted by the binary values of the enumeration constants (that is, by their unsigned magnitude).
Exceptions
- ArgumentException
If length is less than zero.
CreateEnumBitMaskArrayByValue<T>(T)
Creates an array containing elements of the specified enum type T
,
where the length of the array is rounded to the nearest power of two,
which is greater than or equal to the number of enum values.
If the calculated length is greater than the number of enum values,
the remaining elements are filled with a default value.
The idea is to quickly convert an int
value to an enum value by using the array index.
However, the size of the array is limited by a bit mask. If the number of enum values
isn't a power of two, the array is expanded and the additional elements are filled with the default value.
public static T[] CreateEnumBitMaskArrayByValue<T>(T defaultValue) where T : struct, Enum
Parameters
defaultValue
TThe default value that will fill the elements of an array if its size is greater than the number of enum values.
Returns
- T[]
The created array.
Type Parameters
T
The specified enum type.
Remarks
The elements of the array are sorted by the binary values of the enumeration constants (that is, by their unsigned magnitude).
- See Also
GetCountValues<T>()
Gets the number of values for the specified enum type.
public static int GetCountValues<T>() where T : struct, Enum
Returns
- int
Returns the number of values of the specified enum type.
Type Parameters
T
The specified enum type.
ValueOf<T>(T)
Returns an enum constant of the specified enum type with the specified value, or throws ArgumentException if the specified enum type does not have a constant with the specified value.
public static T ValueOf<T>(T value) where T : struct, Enum
Parameters
value
TThe specified value.
Returns
- T
An enum constant of the specified enum type with the specified value.
Type Parameters
T
The specified enum type.
Exceptions
- ArgumentException
If the specified enum type does not have a constant with the specified value.