Skip to content

A base class for quickly and easily creating strongly typed enum replacements in C#.

License

Notifications You must be signed in to change notification settings

mxmissile/SmartEnum

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NuGet

NuGet: Ardalis.SmartEnum

Smart Enum

A simple package with a base Smart Enum class.

Contributors

Thanks for Scott Depouw for his help with this!

Usage

Define your smart enum by inheriting from SmartEnum<TEnum, TValue> where TEnum is the type you're declaring and TValue is the type of its value (typically int). For example:

    public class TestEnum : SmartEnum<TestEnum, int>
    {
        public static TestEnum One = new TestEnum(nameof(One), 1);
        public static TestEnum Two = new TestEnum(nameof(Two), 2);
        public static TestEnum Three = new TestEnum(nameof(Three), 3);

        protected TestEnum(string name, int value) : base(name, value)
        {
        }
    }

List

You can list all of the available options using the enum's static List property:

    var allOptions = TestEnum.List;

FromString

Access an instance of an enum by matching a string to its Name property:

    var myEnum = TestEnum.FromString("One");

FromValue

Access an instance of an enum by matching its value:

    var myEnum = TestEnum.FromValue(1);

ToString

Display an enum using the ToString() override:

    Console.WriteLine(TestEnum.One); // One (1)

References

About

A base class for quickly and easily creating strongly typed enum replacements in C#.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%