Enumem 1.0.5
dotnet add package Enumem --version 1.0.5
NuGet\Install-Package Enumem -Version 1.0.5
<PackageReference Include="Enumem" Version="1.0.5" />
<PackageVersion Include="Enumem" Version="1.0.5" />
<PackageReference Include="Enumem" />
paket add Enumem --version 1.0.5
#r "nuget: Enumem, 1.0.5"
#:package Enumem@1.0.5
#addin nuget:?package=Enumem&version=1.0.5
#tool nuget:?package=Enumem&version=1.0.5
Enumem
Enumem is a lightweight .NET library that provides safe and convenient extensions for working with enums.
It simplifies parsing, retrieving descriptions, listing values, and converting enums into dictionaries, making enum handling more expressive and reliable.
Installation
Install the package from NuGet:
dotnet add package Enumem
Basic Usage
UserRole.cs:
using System.ComponentModel;
using Enumem;
public enum UserRole
{
[Description("System Administrator")]
Admin = 1,
[Description("Regular User")]
User = 2,
[Description("Guest Account")]
Guest = 3
}
GetValue
- Retrieves the numeric (underlying) value of an enum member.
- Useful when you need the integer representation.
var numericValue = UserRole.User.GetValue();
var numericValue2 = Enumem.GetValue(UserRole.User);
Console.WriteLine($"numericValue --> {numericValue}");
Console.WriteLine($"numericValue2 --> {numericValue2}");
numericValue --> 2
numericValue2 --> 2
GetValues
- Returns a list of all enum values defined in the type.
- Ideal for iteration or populating dropdowns.
var values = Enumem.GetValues<UserRole>().ToList();
Console.WriteLine($"values --> {string.Join(", ", values)}");
values --> [Admin, User, Guest]
GetAllNames
- Returns all enum member names as strings.
- Helpful for reflection or dynamic mapping.
var names = Enumem.GetAllNames<UserRole>().ToList();
Console.WriteLine($"names --> {string.Join(", ", names)}");
names --> ["Admin", "User", "Guest"]
GetDescription
- Retrieves the human-readable description assigned to an enum value.
- Useful for displaying friendly names in UI or logs.
var description = UserRole.Admin.GetDescription();
var description2 = Enumem.GetDescription(UserRole.Admin);
Console.WriteLine($"description --> {description}");
Console.WriteLine($"description2 --> {description2}");
"description --> System Administrator"
"description2 --> System Administrator"
GetAllDescriptions
- Returns all description attributes defined for the enum members.
- Useful for displaying descriptive labels in UI components.
var descriptions = Enumem.GetAllDescriptions<UserRole>().ToList();
Console.WriteLine($"descriptions --> {string.Join(", ", descriptions)}");
descriptions --> ["System Administrator", "Regular User", "Guest Account"]
GetEnumFromDescription
Converts a description string back into its corresponding enum value. Useful when mapping user-facing labels to internal enum values.
var roleFromDesc = Enumem.GetEnumFromDescription<UserRole>("Regular User");
Console.WriteLine($"roleFromDesc --> {roleFromDesc}");
roleFromDesc --> User
ToDictionary
- Builds a dictionary mapping each enum’s numeric value to its description.
- Perfect for quick lookups or serialization.
var dict = Enumem.ToDictionary<UserRole>();
Console.WriteLine($"dict --> {string.Join(", ", dict)}");
dict --> [ 1, "System Administrator"], [ 2, "Regular User" ], [ 3, "Guest Account" ]
var dict2 = Enumem.ToDictionary<UserRole>();
foreach (var kvp in dict2)
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
//1: System Administrator
//2: Regular User
//3: Guest Account
IsDefined
- Checks whether a given string matches a valid enum name.
- Prevents invalid conversions or runtime errors.
var isDefined = Enumem.IsDefined<UserRole>("Admin");
Console.WriteLine($"isDefined --> {isDefined}");
isDefined --> true
TryParseEnum
- Safely attempts to convert a string into its corresponding enum value.
- Returns true if the conversion succeeds, false otherwise.
var success = Enumem.TryParseEnum("Admin", out UserRole role1);
Console.WriteLine($"Success --> {success}");
Success --> true
✨ Features
- Safe parsing of enums with
TryParseEnum - Retrieve enum descriptions via
[Description]attribute - List all enum values and names easily
- Convert enums into dictionaries (numeric value → description)
- Check if a name is defined in the enum
- Reverse lookup: get enum from its description
Support the Project
If you find this library useful, consider supporting its development:
⚖️ License
This project is freely available under the MIT license.
You may use it without restrictions, as long as you retain the reference to the original license.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.