Skip to content

Enum Extensions

Andrzej Kebab edited this page Jan 21, 2024 · 1 revision

UtilityLibrary.Core

This utility library provides extension methods for enumerations to perform various operations.

Flag

ClearFlag

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);

Parameters

  • enumValue (T): The enumeration value.
  • flag (T): The flag to clear.

Returns

  • T: The enumeration value with the flag cleared.

ClearFlags

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);

Parameters

  • enumValue (T): The enumeration value.
  • flags (params T[]): The flags to clear.

Returns

  • T: The enumeration value with the flags cleared.

SetFlag

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);

Parameters

  • enumValue (T): The enumeration value.
  • flag (T): The flag to set.

Returns

  • T: The enumeration value with the flag set.

SetFlags

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);

Parameters

  • enumValue (T): The enumeration value.
  • flags (params T[]): The flags to set.

Returns

  • T: The enumeration value with the flags set.

ContainsFlag

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);

Parameters

  • enumValue (T): The enumeration value.
  • flag (T): The flag to check for.

Returns

  • bool: True if the enumeration value contains the flag; otherwise, false.

String

FromString

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);

Parameters

  • enumValue (T): The enumeration value.
  • strValue (string): The string to convert.

Returns

  • T: The enumeration value that corresponds to the string, or the default enumeration value if the string is not defined in the enumeration.

TryParse

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);

Parameters

  • 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.

Returns

  • bool: True if the string was converted successfully; otherwise, false.

Index

EnumIndex

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);

Parameters

  • enumValue (T): The enumeration value.
  • intValue (int): The integer value to find.

Returns

  • int: The index of the integer value in the enumeration, or -1 if the value is not found.

EnumIndex (byte)

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);

Parameters

  • enumValue (T): The enumeration value.
  • byteValue (byte): The byte value to find.

Returns

  • int: The index of the byte value in the enumeration, or -1 if the value is not found.

Clone this wiki locally