Table of Contents

Class EnumHelper

Namespace
SunamoEnumsHelper
Assembly
SunamoEnumsHelper.dll

Provides helper methods for working with enums, including parsing, conversion, flag operations, and value retrieval.

public static class EnumHelper
Inheritance
EnumHelper
Inherited Members

Methods

EnumToString<T>(Type)

Converts an enum type to a dictionary mapping enum values to their lowercase string representations.

public static Dictionary<T, string> EnumToString<T>(Type type) where T : notnull

Parameters

type Type

The enum type to convert.

Returns

Dictionary<T, string>

A dictionary mapping enum values to lowercase string names.

Type Parameters

T

The enum value type.

EnumToString<T>(T)

Converts enum flags to a comma-separated string representation.

public static string EnumToString<T>(T enumValue) where T : Enum

Parameters

enumValue T

The enum value to convert.

Returns

string

A comma-separated string of enum flag names, excluding "Nope" values.

Type Parameters

T

The enum type.

GetAllCombinations<T>(bool)

Gets all enum combinations without zero and All values.

public static List<T> GetAllCombinations<T>(bool isSecondAll = true) where T : struct

Parameters

isSecondAll bool

If true, starts from index [1]. Otherwise from [0].

Returns

List<T>

A list of all valid enum combinations.

Type Parameters

T

The enum type.

GetAllValues<T>(bool)

Gets all enum values without zero and All. If isSecondAll is true, will start from [1]. Otherwise from [0].

public static List<T> GetAllValues<T>(bool isSecondAll = true) where T : struct

Parameters

isSecondAll bool

If true, starts from index [1]. Otherwise from [0].

Returns

List<T>

A list of all enum values excluding zero and All.

Type Parameters

T

The enum type.

GetEnumList<T>(List<T>, List<string>)

Parses a list of string values into enum values. Get values include zero and All. If parsing fails or list is null, returns the default list. Duplicates are added only once.

public static List<T> GetEnumList<T>(List<T> defaultValue, List<string> valuesToParse) where T : struct

Parameters

defaultValue List<T>

The default list to return if parsing fails.

valuesToParse List<string>

The list of string values to parse into enum values.

Returns

List<T>

A list of parsed enum values, or the default list if parsing fails.

Type Parameters

T

The enum type.

GetFlags<T>(T)

Gets all flag names that are set in the specified enum value.

public static List<string> GetFlags<T>(T key) where T : Enum

Parameters

key T

The enum value to get flags from.

Returns

List<string>

A list of string names for all flags that are set.

Type Parameters

T

The enum type.

GetNames(Type)

Gets all enum value names for the specified enum type.

public static List<string> GetNames(Type type)

Parameters

type Type

The enum type to get names from.

Returns

List<string>

A list of all enum value names.

GetValues<T>()

Gets all enum values excluding "Nope" and "None" values. Can be used only for int enums. For more control, use the overload with parameters.

public static List<T> GetValues<T>() where T : struct

Returns

List<T>

A list of enum values excluding "Nope" and "None".

Type Parameters

T

The enum type.

GetValues<T>(bool, bool)

Gets all enum values with options to exclude Nope/None and Shared values.

public static List<T> GetValues<T>(bool isIncludingNope, bool isIncludingShared) where T : struct

Parameters

isIncludingNope bool

Whether to include "Nope" and "None" values.

isIncludingShared bool

Whether to include "Shared" or "Sha" values.

Returns

List<T>

A list of enum values based on the specified inclusion criteria.

Type Parameters

T

The enum type.

ParseFromNumber<T, Number>(Number, T)

Parses a numeric value to an enum value. When trying to cast a number to an enum where this number doesn't exist, it casts and returns the number as string.

public static T ParseFromNumber<T, Number>(Number numericValue, T defaultValue) where T : struct

Parameters

numericValue Number

The numeric value to convert to enum.

defaultValue T

The default enum value to return if conversion fails.

Returns

T

The parsed enum value or the default value if conversion fails.

Type Parameters

T

The enum type to parse to.

Number

The numeric type of the input value.

ParseNullable<T>(string, T?)

Parses a string to a nullable enum value, ignoring case.

public static T? ParseNullable<T>(string text, T? defaultValue) where T : struct

Parameters

text string

The string to parse.

defaultValue T?

The default nullable value to return if parsing fails.

Returns

T?

The parsed enum value or the default value if parsing fails.

Type Parameters

T

The enum type to parse to.

Parse<T>(string, T, bool)

Parses a string to an enum value, ignoring case. Default value must be provided - default(T) cannot be returned because in comparing default(T) is always true for any value of T.

public static T Parse<T>(string text, T defaultValue, bool isReturningDefIfNull = false) where T : struct

Parameters

text string

The string to parse.

defaultValue T

The default enum value to return if parsing fails.

isReturningDefIfNull bool

If true, returns the default value immediately without parsing.

Returns

T

The parsed enum value or the default value if parsing fails.

Type Parameters

T

The enum type to parse to.