InfiniteEnumFlags 0.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package InfiniteEnumFlags --version 0.2.1
NuGet\Install-Package InfiniteEnumFlags -Version 0.2.1
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="InfiniteEnumFlags" Version="0.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add InfiniteEnumFlags --version 0.2.1
#r "nuget: InfiniteEnumFlags, 0.2.1"
#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 InfiniteEnumFlags as a Cake Addin
#addin nuget:?package=InfiniteEnumFlags&version=0.2.1

// Install InfiniteEnumFlags as a Cake Tool
#tool nuget:?package=InfiniteEnumFlags&version=0.2.1

InfiniteEnumFlags

GitHub Nuget Nuget (with prereleases) GitHub Workflow Status NuGet version (InfiniteEnumFlags)

Status: (Project is in active development)

The dotnet enum flags feature is amazing, but it is too limited 🙁. InfiniteEnumFlags is the same without limitation. 😊

Introduction

Dotnet Enum has an [Flags] attribute that gives us the ability to have a binary enum system and use bitwise operators. However, enum specifically restricts to built-in numeric types which is a big problem because it is limited to 2^32 for int values which means we only can have a maximum of 32 items in our enum or 2^64 for long which limits us to a maximum of 64 items.

this library aims to remove these restrictions and still give us the same functionality.

Getting started

To define an enum class, you can create a class/record and inherit it from InfiniteEnum<ClassName>, this base class, later on, will help you to access the list of defined enum items. then by adding static fields of type Flag<ClassName> you can create your enums. setting the values is imperative to provide values as powers of two.

e.g

public class Features : InfiniteEnum<Features>
{
    public static readonly Flag<Features> None = new(-1);  // 0  - 0
    public static readonly Flag<Features> F1 = new(0);     // 1  - 1
    public static readonly Flag<Features> F2 = new(1);     // 2  - 10
    public static readonly Flag<Features> F3 = new(2);     // 4  - 100
    public static readonly Flag<Features> F4 = new(3);     // 8  - 1000
    public static readonly Flag<Features> F5 = new(4);     // 16 - 10000
    public static readonly Flag<Features> F6 = new(5);     // 32 - 100000
    public static readonly Flag<Features> F7 = new(6);     // 64 - 1000000
    public static readonly Flag<Features> F8 = new(7);     // 128 - 10000000

    // We can support up to 2,147,483,647 items
}

Usage

To use your custom enum, it is important to be familiar with the built-in dotnet enum flags capabilities because the functionalities are almost identical. for example we can use all bitwise operators (|,&,~,^) in our custom enum.

e.g

var features = Features.F1 | Features.F3;  // F1 + F3 

Alternatively, If you don't like bitwise Operators, you can use the EnumItem extension methods:

Name Description
HasFlag Check whatever enum has an specific flag or not, (bitwise &)
SetFlag Add/Set specific flag(s) to an enum, (bitwise or)
UnsetFlag Remove/Unset specific flag(s) from an enum (bitwise &~)
ToggleFlag It toggles flag(s) from an enum (bitwise ^)

e.g

features.HasFlag(Features.F2); // false

Storing EnumItem's value

Since we want to support more than 32 items in our enums, we can not store an integer value, luckily we can use EnumItem ToBase64Key() function to get a unique base64 key, and to convert it back to an EnumItem we can use EnumItem.FromBase64() static method.

var features = Features.F1.SetFlag(Features.F3); 
string key = features.ToBase64Key();
var new_features = Features.FromBase64(key); 
Console.WriteLine(features == new_features); // true

Example

To see flexible aspnetcore authorization example using InfiniteEnumFlags, first clone the repo using below command

 git clone --recurse-submodules https://github.com/alirezanet/InfiniteEnumFlags.git

then you can open \InfiniteEnumFlags\Example\flexible-aspnetcore-authorization\FlexibleAuth\FlexibleAuth.sln and then run the Server project.

Support

  • Don't forget to give a ⭐ on GitHub
  • Share your feedback and ideas to improve this library
  • Share InfiniteEnumFlags on your favorite social media and your friends
  • Write a blog post about InfiniteEnumFlags

Contribution

Feel free to send me a pull request!

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 was computed.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.4.3 5,901 11/30/2023
0.4.2 3,858 3/17/2023
0.4.1 1,014 12/16/2022
0.4.0 325 12/2/2022
0.3.0 387 11/8/2022
0.2.1 321 11/7/2022
0.2.0 354 11/7/2022
0.1.2 377 10/26/2022
0.1.1 373 10/26/2022
0.1.0 385 10/25/2022
0.0.4 380 10/25/2022
0.0.3 367 10/24/2022