Class EnumUtility
public static class EnumUtility
- Inheritance
-
EnumUtility
- Inherited Members
Methods
AssertValidField<TEnum>(TEnum, params ReadOnlySpan<TEnum>)
Check if an enum value is a valid field in the enum declaration (instead of a random number) and is not in the provided list of invalid values.
public static void AssertValidField<TEnum>(TEnum value, params ReadOnlySpan<TEnum> invalids) where TEnum : unmanaged, Enum
Parameters
valueTEnuminvalidsReadOnlySpan<TEnum>List of invalid values which should also throw an ArgumentOutOfRangeException.
Type Parameters
TEnum
Exceptions
- ArgumentOutOfRangeException
Value is not part of a field.
- InvalidOperationException
Invalid enum type.
AssertValidField<TEnum>(TEnum, string)
Check if an enum value is a valid field in the enum declaration (instead of a random number).
public static void AssertValidField<TEnum>(TEnum value, string paramterName = "value") where TEnum : unmanaged, Enum
Parameters
valueTEnumparamterNamestring
Type Parameters
TEnum
Exceptions
- ArgumentOutOfRangeException
Value is not part of a field.
- InvalidOperationException
Invalid enum type.
AssertValidField<TEnum>(TEnum, string, params ReadOnlySpan<TEnum>)
Check if an enum value is a valid field in the enum declaration (instead of a random number) and is not in the provided list of invalid values.
public static void AssertValidField<TEnum>(TEnum value, string paramterName, params ReadOnlySpan<TEnum> invalids) where TEnum : unmanaged, Enum
Parameters
valueTEnumparamterNamestringinvalidsReadOnlySpan<TEnum>List of invalid values which should also throw an ArgumentOutOfRangeException.
Type Parameters
TEnum
Exceptions
- ArgumentOutOfRangeException
Value is not part of a field.
- InvalidOperationException
Invalid enum type.
Compare<TEnum>(TEnum, TEnum)
Compare two generic enums, returning a positive number if a is greater than b, zero if they are equal, or a negative number if a is less than b.
public static int Compare<TEnum>(TEnum a, TEnum b) where TEnum : unmanaged, Enum
Parameters
aTEnumbTEnum
Returns
Type Parameters
TEnum
Exceptions
- InvalidOperationException
Invalid enum type.
Equals<TEnum>(TEnum, TEnum)
Check if a is equal to b in value.
public static bool Equals<TEnum>(TEnum a, TEnum b) where TEnum : unmanaged, Enum
Parameters
aTEnumbTEnum
Returns
Type Parameters
TEnum
Exceptions
- InvalidOperationException
Invalid enum type.
GetEnumValues<TEnum>()
Gets an all declared fields in the enum sorted from lowest to highest.
public static IReadOnlyList<TEnum> GetEnumValues<TEnum>() where TEnum : unmanaged, Enum
Returns
- IReadOnlyList<TEnum>
Type Parameters
TEnum
Exceptions
- InvalidOperationException
Invalid enum type.
GetMaximumValue<TEnum>()
Gets the highest numerical enum value in the given enum type.
public static TEnum GetMaximumValue<TEnum>() where TEnum : unmanaged, Enum
Returns
- TEnum
Type Parameters
TEnum
Exceptions
- InvalidOperationException
Invalid enum type.
GetMinimumValue<TEnum>()
Gets the lowest numerical enum value in the given enum type.
public static TEnum GetMinimumValue<TEnum>() where TEnum : unmanaged, Enum
Returns
- TEnum
Type Parameters
TEnum
Exceptions
- InvalidOperationException
Invalid enum type.
GetNameSafe<TEnum>(TEnum)
Get the name of the enum which is cached after first use. Significantly faster than GetEnumName(object) for repeated use.
public static string GetNameSafe<TEnum>(TEnum value) where TEnum : unmanaged, Enum
Parameters
valueTEnum
Returns
- string
The name of the field represented by the enum, or a comma separated list of all flags if the FlagsAttribute is present.
Type Parameters
TEnum
Remarks
Flag enums will simply call back to ToString() for composite values.
Exceptions
- InvalidOperationException
Invalid enum type.
GetName<TEnum>(TEnum)
Get the name of the enum which is cached after first use. Significantly faster than GetEnumName(object) for repeated use.
public static string? GetName<TEnum>(TEnum value) where TEnum : unmanaged, Enum
Parameters
valueTEnum
Returns
- string
The name of the field represented by the enum, or a comma separated list of all flags if the FlagsAttribute is present.
Type Parameters
TEnum
Remarks
Flag enums will simply call back to ToString() for composite values.
Exceptions
- InvalidOperationException
Invalid enum type.
GreaterOrEqual<TEnum>(TEnum, TEnum)
Check if a is greater than or equal to b in value.
public static bool GreaterOrEqual<TEnum>(TEnum a, TEnum b) where TEnum : unmanaged, Enum
Parameters
aTEnumbTEnum
Returns
Type Parameters
TEnum
Exceptions
- InvalidOperationException
Invalid enum type.
GreaterThan<TEnum>(TEnum, TEnum)
Check if a is greater than b in value.
public static bool GreaterThan<TEnum>(TEnum a, TEnum b) where TEnum : unmanaged, Enum
Parameters
aTEnumbTEnum
Returns
Type Parameters
TEnum
Exceptions
- InvalidOperationException
Invalid enum type.
IsFlag<TEnum>()
Get whether or not an enum type defines the FlagsAttribute. Significantly faster than re-checking for repeated use.
public static bool IsFlag<TEnum>() where TEnum : unmanaged, Enum
Returns
Type Parameters
TEnum
Exceptions
- InvalidOperationException
Invalid enum type.
LessOrEqual<TEnum>(TEnum, TEnum)
Check if a is less than or equal to b in value.
public static bool LessOrEqual<TEnum>(TEnum a, TEnum b) where TEnum : unmanaged, Enum
Parameters
aTEnumbTEnum
Returns
Type Parameters
TEnum
Exceptions
- InvalidOperationException
Invalid enum type.
LessThan<TEnum>(TEnum, TEnum)
Check if a is less than b in value.
public static bool LessThan<TEnum>(TEnum a, TEnum b) where TEnum : unmanaged, Enum
Parameters
aTEnumbTEnum
Returns
Type Parameters
TEnum
Exceptions
- InvalidOperationException
Invalid enum type.
ValidateValidField<TEnum>(TEnum)
Check if an enum value is a valid field in the enum declaration (instead of a random number).
public static bool ValidateValidField<TEnum>(TEnum value) where TEnum : unmanaged, Enum
Parameters
valueTEnum
Returns
Type Parameters
TEnum
Exceptions
- InvalidOperationException
Invalid enum type.