A lightweight type safety library for enum type values and description.
Install Enumify.Net via Nuget
PM> Install-Package Enumify.Net
- netstandard 2.1, netstandard 2.0
- netcoreapp 3.1, netcoreapp 3.0, netcoreapp 2.2, netcoreapp 2.1, netcoreapp 2.0,
- NetFramework 4.8.0
DayOfWeek saturday = Enums.Parse<DayOfWeek>("Saturday");
IList<DayOfWeek> values = Enums.GetValues<DayOfWeek>();
IList<string> names = Enums.GetNames<DayOfWeek>();
string name = DayOfWeek.Saturday.GetName();
bool result = Enums.TryParse("Saturday", out DayOfWeek day);
Assert.True(Enums.TryParse("One", out Number number));
Assert.Equal(Number.One, number);
Assert.False(Enums.TryParse("1", out number));
Assert.Equal((Number)0, number);
Assert.False(Enums.TryParse("foo", out number));
Assert.Equal((Number)0, number);
bool isDefinedAsString = Enums.IsDefined<Number>("One");
bool isDefinedAsInt = Enums.IsDefined<Number>(1);
bool isDefinedAsLong = Enums.IsDefined<Int64Enum>(0x7FFFFFFFFFFFFFFF);
bool isDefinedAsByte = Enums.IsDefined<ByteEnum>(1);
string desc = Number.One.GetDescription();
bool result = Enums.TryParseDescription("Third description", out Number number);
public enum Number
{
[EnumDescription("First description")]
One = 1,
Two = 2,
[EnumDescription("Third description")]
Three = 3
}
[Flags]
public enum Int64Enum : long
{
MinusOne = -1,
Zero = 0,
Max = 0x7FFFFFFFFFFFFFFF
}
Assert.Equal(typeof(byte), Enums.GetUnderlyingType<ByteEnum>());
Assert.Equal(typeof(int), Enums.GetUnderlyingType<Number>());
Assert.Equal(typeof(long), Enums.GetUnderlyingType<Int64Enum>());
Assert.Equal(typeof(ulong), Enums.GetUnderlyingType<UInt64Enum>());
Thanks to Benchmark DotNet library.
Method | Mean | Error | StdDev | Rank | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|
EnumifyDotNet | 0.0000 ns | 0.0000 ns | 0.0000 ns | 1 | - | - | - | - |
DotNet | 855.3133 ns | 16.9984 ns | 42.0158 ns | 2 | 0.0534 | - | - | 224 B |
Method | Mean | Error | StdDev | Median | Rank | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
EnumifyDotNet | 0.4590 ns | 0.2194 ns | 0.6468 ns | 0.0000 ns | 1 | - | - | - | - |
DotNet | 57.0122 ns | 2.0174 ns | 5.7884 ns | 56.0215 ns | 2 | 0.0191 | - | - | 80 B |
Method | Mean | Error | StdDev | Median | Rank | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
Enumify | 89.66 ns | 1.873 ns | 4.766 ns | 89.68 ns | 1 | - | - | - | - |
DotNet | 110.80 ns | 14.266 ns | 42.062 ns | 85.87 ns | 1 | - | - | - | - |
Please check out more samples in the Unit test app.