Skip to content

Latest commit

 

History

History
359 lines (244 loc) · 9.34 KB

DocsReadMe.md

File metadata and controls

359 lines (244 loc) · 9.34 KB

ToAnother

Description

Convert input enum to another enum.

Signature

TAnotherEnum ToAnother(this Enum, bool [default = true]) where TAnotherEnum : Enum

Type Parameters

TAnotherEnum - Enum that we want to get after convert.

Parameters

  • Enum enumValue - Input enum we want to convert to TAnotherEnum.
  • bool ignoreCase - Ignore or regard case.

Returns

TAnotherEnum

Code examples

// Enums
public enum AnotherEnum { First, Second, Third }

public enum InputEnum { First, Second, Third }
public enum InputEnumLowerCase { first, second, third }
public enum InputEnumUpperCase { FIRST, SECOND, THIRD }

// Input enums
InputEnum first = InputEnum.First;
InputEnumLowerCase second = InputEnumLowerCase.second;
InputEnumUpperCase third = InputEnumUpperCase.THIRD;
AnotherEnum anotherEnumFirst = first.ToAnother<AnotherEnum>();
AnotherEnum anotherEnumSecond = second.ToAnother<AnotherEnum>();
AnotherEnum anotherEnumThird = third.ToAnother<AnotherEnum>(true); // true by default

// Output:
// anotherEnumFirst - First
// anotherEnumSecond - Second
// anotherEnumThird - Third
AnotherEnum anotherEnumFirst = first.ToAnother<AnotherEnum>(false);
AnotherEnum anotherEnumSecond = second.ToAnother<AnotherEnum>(false);
AnotherEnum anotherEnumThird = third.ToAnother<AnotherEnum>(false);

// Output:
// anotherEnumFirst - First
// anotherEnumSecond - Throws ArgumentException
// anotherEnumThird - Throws ArgumentException

TryToAnother

Description

Try convert input enum to another enum.

Signature

bool TryToAnother(this Enum, bool [default = true], out TAnotherEnum) where TAnotherEnum : struct

Type Parameters

TAnotherEnum - Enum that we want try to get after convert.

Parameters

  • Enum enumValue - Input enum we want try to convert to TAnotherEnum.
  • bool ignoreCase - Ignore or regard case.
  • out TAnotherEnum anotherEnum - TAnotherEnum or default of TAnotherEnum.

Returns

true if the enumValue parameter was converted successfully; otherwise, false.

Code examples

// Enums
public enum AnotherEnum { First, Second, Third }

public enum InputEnum { First, Second, Third }
public enum InputEnumLowerCase { first, second, third }
public enum InputEnumUpperCase { FIRST, SECOND, THIRD }

public enum InvalidEnum { FirstInvalidValue, SecondInvalidValue }

// Input enums
InputEnum first = InputEnum.First;
InputEnumLowerCase second = InputEnumLowerCase.second;
InputEnumUpperCase third = InputEnumUpperCase.THIRD;
InvalidEnum invalidSecondValue = InvalidEnum.SecondInvalidValue;
bool isConvertedFirst = first.TryToAnother(out AnotherEnum anotherEnumFirst);
bool isConvertedSecond = second.TryToAnother(out AnotherEnum anotherEnumSecond);
bool isConvertedThird = third.TryToAnother(true, out AnotherEnum anotherEnumThird); // true by default

// Output:
// [isConvertedFirst, anotherEnumFirst] - [true, First]
// [isConvertedSecond, anotherEnumSecond] - [true, Second]
// [isConvertedThird, anotherEnumThird] - [true, Third]
bool isConvertedFirst = first.TryToAnother(false, out AnotherEnum anotherEnumFirst);
bool isConvertedSecond = second.TryToAnother(false, out AnotherEnum anotherEnumSecond);
bool isConvertedThird = third.TryToAnother(false, out AnotherEnum anotherEnumThird);

// Output:
// [isConvertedFirst, anotherEnumFirst] - [true, First]
// [isConvertedSecond, anotherEnumSecond] - [false, First]
// [isConvertedThird, anotherEnumThird] - [false, First]
bool isConverted = invalidSecondValue.TryToAnother(out AnotherEnum anotherEnum);

// Output:
// [isConverted, anotherEnum] - [false, First]

ToAnotherOrDefault

Description

Convert input enum to another enum or default value if not possible.

Signature

TAnotherEnum ToAnotherOrDefault(this Enum, bool [default = true], TAnotherEnum = default(TAnotherEnum)) where TAnotherEnum : struct

Type Parameters

TAnotherEnum - Enum that we want try to get after convert or defaultValue if not possible.

Parameters

  • Enum enumValue - Input enum we want try to convert to TAnotherEnum or defaultValue if not possible.
  • bool ignoreCase - Ignore or regard case.
  • defaultValue - The default value to return when cannot convert enumValue to TAnotherEnum.

Returns

TAnotherEnum or defaultValue if not possible to convert.

Code examples

// Enums.
public enum AnotherEnum { First, Second, Third }

public enum InputEnum { First, Second, Third }
public enum InvalidEnum { FirstInvalidValue, SecondInvalidValue }


// Input enums
InputEnum inputEnumFirst = InputEnum.First;
InvalidEnum invalidSecondValue = InvalidEnum.SecondInvalidValue;
AnotherEnum customDefaultValue = AnotherEnum.Third;
InputEnum inputEnumThird = InputEnum.Third;
AnotherEnum anotherEnumFirstCorrect = inputEnumFirst.ToAnotherOrDefault<AnotherEnum>();
AnotherEnum anotherEnumSecondInvalid = invalidSecondValue.ToAnotherOrDefault<AnotherEnum>();
AnotherEnum anotherEnumWithDefaultValue = invalidSecondValue.ToAnotherOrDefault<AnotherEnum>(customDefaultValue);
AnotherEnum anotherEnumThirdWithIgnoreCase = inputEnumThird.ToAnotherOrDefault<AnotherEnum>(false);

// Output:
// anotherEnumFirstCorrect - First
// anotherEnumSecondInvalid - default(AnotherEnum)
// anotherEnumWithDefaultValue - Third (customDefaultValue)
// anotherEnumThirdWithIgnoreCase - Third

ToOther

Description

Convert input enums to other enums.

Signature

IEnumerable ToOther<TInputEnum, TOutputEnum>(this IEnumerable, bool [default = true]) where TInputEnum, TOutputEnum : Enum

Type Parameters

TInputEnum - Type of enumValues TOutputEnum - Enum type that we want to get after convert.

Parameters

  • IEnumerable<TInputEnum> enumValues - The input enums that we want to convert to enumerable of TOutputEnum.
  • bool ignoreCase - Ignore or regard case.

Returns

IEnumerable<TOutputEnum>

Code examples

// Enums
public enum InputEnum { First, Second, Third }
public enum OutputEnum { First, Second, Third }
IEnumerable<InputEnum> inputEnums = new List<InputEnum>()
{
    InputEnum.First, InputEnum.Second, InputEnum.Third,
};
IEnumerable<OutputEnum> output = inputEnums.ToOther<InputEnum, OutputEnum>(); // or false if case is important.

// output
// 1 - OutputEnum.First
// 2 - OutputEnum.Second
// 3 - OutputEnum.Third

TryToEnum

Description

Try convert string value to enum.

Signature

bool TryToEnum(this string, bool [default = true] out TEnum) where TEnum : struct, Enum

Type Parameters

TEnum - Enum that we want try to get after convert.

Parameters

  • string stringValue - Input string we want to convert to TEnum.
  • bool ignoreCase - Ignore or regard case.
  • out TEnum outputEnum - TEnum or default of TEnum.

Returns

true if the stringValue parameter was converted successfully; otherwise, false.

Code examples

public enum MyEnum { First, Second, Third }
string stringValueFirst = "First";
string stringValueSecond = "Second";
string stringValueInvalid = "Blablabla";

bool isConvertedFirst = stringValueFirst.TryToEnum(out MyEnum enumFirst);
bool isConvertedSecond = stringValueSecond.TryToEnum(false, out MyEnum enumSecond);
bool isConvertedThird = stringValueInvalid.TryToEnum(out MyEnum enumThird);

// output
// [isConvertedFirst, enumFirst] - [true, MyEnum.First]
// [isConvertedSecond, enumSecond] - [true, MyEnum.Second]
// [isConvertedThird, enumThird] - [false, MyEnum.First]

ToEnumOrDefault

Description

Try convert string value to enum or return default value if not possible.

Signature

bool ToEnumOrDefault(this string, bool [default = true], TEnum [default = TEnum]) where TEnum : struct, Enum

Type Parameters

TEnum - Enum that we want try to get after convert.

Parameters

  • string stringValue - Input string we want to convert to TEnum.
  • bool ignoreCase - Ignore or regard case.
  • TEnum defaultValue - TEnum or default of TEnum.

Returns

TEnum or defaultValue if not possible to convert.

Code examples

public enum MyEnum { First, Second, Third }
string stringValueFirst = "First";
string stringValueSecond = "Second";
string stringValueInvalid = "Blablabla";

MyEnum enumFirst = stringValueFirst.ToEnumOrDefault<MyEnum>();
MyEnum enumSecond = stringValueSecond.ToEnumOrDefault<MyEnum>(false);
MyEnum enumThird = stringValueInvalid.ToEnumOrDefault<MyEnum>(MyEnum.Third);

// output
// enumFirst - MyEnum.First
// enumSecond - MyEnum.Second
// enumThird - MyEnum.Third

ToEnums

Description

Convert input string to enums.

Signature

IEnumerable ToEnums(this IEnumerable, bool [default = true]) where TEnum : Enum

Type Parameters

TEnum - Enum type that we want to get after convert.

Parameters

  • IEnumerable<string> stringValues - The input string that we want to convert to enumerable of TEnum.
  • bool ignoreCase - Ignore or regard case.

Returns

IEnumerable<TEnum>

Code examples

// Enums
public enum MyEnum { First, Second, Third }
IEnumerable<string> inputEnums = new List<string>()
{
    "First", "Second", "Third",
};
IEnumerable<MyEnum> output = inputEnums.ToEnums<MyEnum>(); // or false if case is important.

// output
// 1 - MyEnum.First
// 2 - MyEnum.Second
// 3 - MyEnum.Third