Enums.NET 5.0.0

dotnet add package Enums.NET --version 5.0.0                
NuGet\Install-Package Enums.NET -Version 5.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Enums.NET" Version="5.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Enums.NET --version 5.0.0                
#r "nuget: Enums.NET, 5.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Enums.NET as a Cake Addin
#addin nuget:?package=Enums.NET&version=5.0.0

// Install Enums.NET as a Cake Tool
#tool nuget:?package=Enums.NET&version=5.0.0                

Enums.NET is a high-performance type-safe .NET enum utility library which provides many operations as convenient extension methods. It is compatible with .NET Framework 4.6.1+ and .NET Standard 2.0+.

Enums.NET Demo

using System;
using System.Linq;
using EnumsNET;
using Xunit;
using DescriptionAttribute = System.ComponentModel.DescriptionAttribute;

public class EnumsNETDemo
{
    // Enum definitions at bottom

    [Fact]
    public void Enumerate()
    {
        var count = 0;
        // Retrieves all enum members in increasing value order
        foreach (var member in Enums.GetMembers<NumericOperator>())
        {
            NumericOperator value = member.Value;
            string name = member.Name;
            AttributeCollection attributes = member.Attributes;
            ++count;
        }
        Assert.Equal(8, count);

        count = 0;
        // Retrieves distinct values in increasing value order
        foreach (var value in Enums.GetValues<NumericOperator>(EnumMemberSelection.Distinct))
        {
            string name = value.GetName();
            AttributeCollection attributes = value.GetAttributes();
            ++count;
        }
        Assert.Equal(6, count);
    }

    [Fact]
    public void FlagEnumOperations()
    {
        // HasAllFlags
        Assert.True((DaysOfWeek.Monday | DaysOfWeek.Wednesday | DaysOfWeek.Friday).HasAllFlags(DaysOfWeek.Monday | DaysOfWeek.Wednesday));
        Assert.False(DaysOfWeek.Monday.HasAllFlags(DaysOfWeek.Monday | DaysOfWeek.Wednesday));

        // HasAnyFlags
        Assert.True(DaysOfWeek.Monday.HasAnyFlags(DaysOfWeek.Monday | DaysOfWeek.Wednesday));
        Assert.False((DaysOfWeek.Monday | DaysOfWeek.Wednesday).HasAnyFlags(DaysOfWeek.Friday));

        // CombineFlags ~ bitwise OR
        Assert.Equal(DaysOfWeek.Monday | DaysOfWeek.Wednesday, DaysOfWeek.Monday.CombineFlags(DaysOfWeek.Wednesday));
        Assert.Equal(DaysOfWeek.Monday | DaysOfWeek.Wednesday | DaysOfWeek.Friday, FlagEnums.CombineFlags(DaysOfWeek.Monday, DaysOfWeek.Wednesday, DaysOfWeek.Friday));

        // CommonFlags ~ bitwise AND
        Assert.Equal(DaysOfWeek.Monday, DaysOfWeek.Monday.CommonFlags(DaysOfWeek.Monday | DaysOfWeek.Wednesday));
        Assert.Equal(DaysOfWeek.None, DaysOfWeek.Monday.CommonFlags(DaysOfWeek.Wednesday));

        // RemoveFlags
        Assert.Equal(DaysOfWeek.Wednesday, (DaysOfWeek.Monday | DaysOfWeek.Wednesday).RemoveFlags(DaysOfWeek.Monday));
        Assert.Equal(DaysOfWeek.None, (DaysOfWeek.Monday | DaysOfWeek.Wednesday).RemoveFlags(DaysOfWeek.Monday | DaysOfWeek.Wednesday));

        // GetFlags, splits out the individual flags in increasing significance bit order
        var flags = DaysOfWeek.Weekend.GetFlags().ToList();
        Assert.Equal(2, flags.Count);
        Assert.Equal(DaysOfWeek.Sunday, flags[0]);
        Assert.Equal(DaysOfWeek.Saturday, flags[1]);
    }

    [Fact]
    public void AsString()
    {
        // AsString, equivalent to ToString
        Assert.Equal("Equals", NumericOperator.Equals.AsString());
        Assert.Equal("-1", ((NumericOperator)(-1)).AsString());

        // GetName
        Assert.Equal("Equals", NumericOperator.Equals.GetName());
        Assert.Null(((NumericOperator)(-1)).GetName());

        // Get description
        Assert.Equal("Is", NumericOperator.Equals.AsString(EnumFormat.Description));
        Assert.Null(NumericOperator.LessThan.AsString(EnumFormat.Description));

        // Get description if applied, otherwise the name
        Assert.Equal("LessThan", NumericOperator.LessThan.AsString(EnumFormat.Description, EnumFormat.Name));
    }

    [Fact]
    public void Validate()
    {
        // Standard Enums, checks is defined
        Assert.True(NumericOperator.LessThan.IsValid());
        Assert.False(((NumericOperator)20).IsValid());

        // Flag Enums, checks is valid flag combination or is defined
        Assert.True((DaysOfWeek.Sunday | DaysOfWeek.Wednesday).IsValid());
        Assert.False((DaysOfWeek.Sunday | DaysOfWeek.Wednesday | ((DaysOfWeek)(-1))).IsValid());

        // Custom validation through IEnumValidatorAttribute<TEnum>
        Assert.True(DayType.Weekday.IsValid());
        Assert.True((DayType.Weekday | DayType.Holiday).IsValid());
        Assert.False((DayType.Weekday | DayType.Weekend).IsValid());
    }

    [Fact]
    public void CustomEnumFormat()
    {
        EnumFormat symbolFormat = Enums.RegisterCustomEnumFormat(member => member.Attributes.Get<SymbolAttribute>()?.Symbol);
        Assert.Equal(">", NumericOperator.GreaterThan.AsString(symbolFormat));
        Assert.Equal(NumericOperator.LessThan, Enums.Parse<NumericOperator>("<", ignoreCase: false, symbolFormat));
    }

    [Fact]
    public void Attributes()
    {
        Assert.Equal("!=", NumericOperator.NotEquals.GetAttributes().Get<SymbolAttribute>().Symbol);
        Assert.True(Enums.GetMember<NumericOperator>("GreaterThanOrEquals").Attributes.Has<PrimaryEnumMemberAttribute>());
        Assert.False(NumericOperator.LessThan.GetAttributes().Has<DescriptionAttribute>());
    }

    [Fact]
    public void Parsing()
    {
        Assert.Equal(NumericOperator.GreaterThan, Enums.Parse<NumericOperator>("GreaterThan"));
        Assert.Equal(NumericOperator.NotEquals, Enums.Parse<NumericOperator>("1"));
        Assert.Equal(NumericOperator.Equals, Enums.Parse<NumericOperator>("Is", ignoreCase: false, EnumFormat.Description));

        Assert.Equal(DaysOfWeek.Monday | DaysOfWeek.Wednesday, Enums.Parse<DaysOfWeek>("Monday, Wednesday"));
        Assert.Equal(DaysOfWeek.Tuesday | DaysOfWeek.Thursday, FlagEnums.ParseFlags<DaysOfWeek>("Tuesday | Thursday", ignoreCase: false, delimiter: "|"));
    }

    enum NumericOperator
    {
        [Symbol("="), Description("Is")]
        Equals,
        [Symbol("!="), Description("Is not")]
        NotEquals,
        [Symbol("<")]
        LessThan,
        [Symbol(">="), PrimaryEnumMember] // PrimaryEnumMember indicates enum member as primary duplicate for extension methods
        GreaterThanOrEquals,
        NotLessThan = GreaterThanOrEquals,
        [Symbol(">")]
        GreaterThan,
        [Symbol("<="), PrimaryEnumMember]
        LessThanOrEquals,
        NotGreaterThan = LessThanOrEquals
    }

    [AttributeUsage(AttributeTargets.Field)]
    class SymbolAttribute : Attribute
    {
        public string Symbol { get; }

        public SymbolAttribute(string symbol)
        {
            Symbol = symbol;
        }
    }

    [Flags]
    enum DaysOfWeek
    {
        None = 0,
        Sunday = 1,
        Monday = 2,
        Tuesday = 4,
        Wednesday = 8,
        Thursday = 16,
        Friday = 32,
        Weekdays = Monday | Tuesday | Wednesday | Thursday | Friday,
        Saturday = 64,
        Weekend = Sunday | Saturday,
        All = Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday
    }

    [Flags, DayTypeValidator]
    enum DayType
    {
        Weekday = 1,
        Weekend = 2,
        Holiday = 4
    }

    class DayTypeValidatorAttribute : EnumValidatorAttribute<DayType>
    {
        public override bool IsValid(DayType value) => value.GetFlagCount(DayType.Weekday | DayType.Weekend) == 1 && FlagEnums.IsValidFlagCombination(value);
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 is compatible.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (99)

Showing the top 5 NuGet packages that depend on Enums.NET:

Package Downloads
NPOI

.NET port of Apache POI

Xpand.Extensions.XAF

Xpand.Extensions.XAF

Xpand.XAF.Modules.Reactive

The `Reactive` module provides a XAF DSL API for functional/stateless implementations.

Xpand.Extensions.Reactive

Xpand.Extensions.Reactive

Xpand.XAF.Modules.ModelMapper

The `ModelMapper` allows to control all XAF components from the application model.

GitHub repositories (14)

Showing the top 5 popular GitHub repositories that depend on Enums.NET:

Repository Stars
nissl-lab/npoi
a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
opserver/Opserver
Stack Exchange's Monitoring System
ardalis/SmartEnum
A base class for quickly and easily creating strongly typed enum replacements in C#.
TylerBrinkley/Enums.NET
Enums.NET is a high-performance type-safe .NET enum utility library
PoESkillTree/PoESkillTree
A Passive Skill Tree Planner for Path of Exile
Version Downloads Last updated
5.0.0 770,108 5/3/2024
4.0.2 715,734 1/17/2024
4.0.1 8,873,097 11/18/2022
4.0.0 9,543,581 1/29/2021
3.0.3 2,841,959 1/23/2020
3.0.2 186,729 12/6/2019
3.0.1 229,145 11/11/2019
3.0.0 125,993 11/2/2019
2.3.2 6,397,440 6/23/2018
2.3.1 171,683 12/15/2017
2.3.0 36,218 11/21/2017
2.2.0 70,757 6/6/2017
2.1.1 30,634 3/25/2017
2.0.0 131,507 1/1/2017
1.0.0 9,362 11/5/2016