-
Notifications
You must be signed in to change notification settings - Fork 0
Enum Extensions
Andrzej Kebab edited this page Jan 21, 2024
·
1 revision
This utility library provides extension methods for enumerations to perform various operations.
Clears a specific flag from an enumeration value.
using UtilityLibrary.Core;
[Flags]
public enum MyFlagsEnum
{
FlagA = 1,
FlagB = 2,
FlagC = 4
}
MyFlagsEnum enumValue = MyFlagsEnum.FlagA | MyFlagsEnum.FlagB;
MyFlagsEnum result = enumValue.ClearFlag(MyFlagsEnum.FlagB);-
enumValue(T): The enumeration value. -
flag(T): The flag to clear.
- T: The enumeration value with the flag cleared.
Clears specific flags from an enumeration value.
using UtilityLibrary.Core;
[Flags]
public enum MyFlagsEnum
{
FlagA = 1,
FlagB = 2,
FlagC = 4
}
MyFlagsEnum enumValue = MyFlagsEnum.FlagA | MyFlagsEnum.FlagB | MyFlagsEnum.FlagC;
MyFlagsEnum result = enumValue.ClearFlags(MyFlagsEnum.FlagB, MyFlagsEnum.FlagC);-
enumValue(T): The enumeration value. -
flags(params T[]): The flags to clear.
- T: The enumeration value with the flags cleared.
Sets a specific flag in an enumeration value.
using UtilityLibrary.Core;
[Flags]
public enum MyFlagsEnum
{
FlagA = 1,
FlagB = 2,
FlagC = 4
}
MyFlagsEnum enumValue = MyFlagsEnum.FlagA;
MyFlagsEnum result = enumValue.SetFlag(MyFlagsEnum.FlagB);-
enumValue(T): The enumeration value. -
flag(T): The flag to set.
- T: The enumeration value with the flag set.
Sets specific flags in an enumeration value.
using UtilityLibrary.Core;
[Flags]
public enum MyFlagsEnum
{
FlagA = 1,
FlagB = 2,
FlagC = 4
}
MyFlagsEnum enumValue = MyFlagsEnum.FlagA;
MyFlagsEnum result = enumValue.SetFlags(MyFlagsEnum.FlagB, MyFlagsEnum.FlagC);-
enumValue(T): The enumeration value. -
flags(params T[]): The flags to set.
- T: The enumeration value with the flags set.
Checks if an enumeration value contains a specific flag.
using UtilityLibrary.Core;
[Flags]
public enum MyFlagsEnum
{
FlagA = 1,
FlagB = 2,
FlagC = 4
}
MyFlagsEnum enumValue = MyFlagsEnum.FlagA | MyFlagsEnum.FlagB;
bool containsFlag = enumValue.ContainsFlag(MyFlagsEnum.FlagB);-
enumValue(T): The enumeration value. -
flag(T): The flag to check for.
- bool: True if the enumeration value contains the flag; otherwise, false.
Converts a string to an enumeration value of a specified type.
using UtilityLibrary.Core;
public enum MyEnum
{
ValueA,
ValueB,
ValueC
}
MyEnum enumValue = MyEnum.ValueA;
string strValue = "ValueB";
MyEnum result = enumValue.FromString(strValue);-
enumValue(T): The enumeration value. -
strValue(string): The string to convert.
- T: The enumeration value that corresponds to the string, or the default enumeration value if the string is not defined in the enumeration.
Tries to convert a string to an enumeration value of a specified type.
using UtilityLibrary.Core;
public enum MyEnum
{
ValueA,
ValueB,
ValueC
}
MyEnum enumValue = MyEnum.ValueA;
string strValue = "ValueB";
MyEnum result;
bool success = enumValue.TryParse(strValue, out result);-
enumValue(T): The enumeration value. -
strValue(string): The string to convert. -
returnValue(out T): When this method returns, contains the enumeration value that corresponds to the string, if the conversion succeeded, or the default enumeration value if the conversion failed.
- bool: True if the string was converted successfully; otherwise, false.
Returns the index of a specific integer value in an enumeration.
using UtilityLibrary.Core;
public enum MyEnum
{
ValueA = 1,
ValueB = 2,
ValueC = 3
}
MyEnum enumValue = MyEnum.ValueB;
int intValue = 2;
int index = enumValue.EnumIndex(intValue);-
enumValue(T): The enumeration value. -
intValue(int): The integer value to find.
- int: The index of the integer value in the enumeration, or -1 if the value is not found.
Returns the index of a specific byte value in an enumeration.
using UtilityLibrary.Core;
public enum MyEnum : byte
{
ValueA = 1,
ValueB = 2,
ValueC = 3
}
MyEnum enumValue = MyEnum.ValueB;
byte byteValue = 2;
int index = enumValue.EnumIndex(byteValue);-
enumValue(T): The enumeration value. -
byteValue(byte): The byte value to find.
- int: The index of the byte value in the enumeration, or -1 if the value is not found.