InfiniteEnumFlags 0.1.1
See the version list below for details.
dotnet add package InfiniteEnumFlags --version 0.1.1
NuGet\Install-Package InfiniteEnumFlags -Version 0.1.1
<PackageReference Include="InfiniteEnumFlags" Version="0.1.1" />
paket add InfiniteEnumFlags --version 0.1.1
#r "nuget: InfiniteEnumFlags, 0.1.1"
// Install InfiniteEnumFlags as a Cake Addin #addin nuget:?package=InfiniteEnumFlags&version=0.1.1 // Install InfiniteEnumFlags as a Cake Tool #tool nuget:?package=InfiniteEnumFlags&version=0.1.1
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
After installing the InfiniteEnumFlags NuGet package, there are several ways to use this package. I start with the easiest one.
Define your enum flags
1. using source generator (Recommended)
To define your enum, you must create a partial
class and extend it using IArrayFlags
or IIndexDictionaryFlags
and Implement the Items
function that returns a list of strings.
IArrayFlags
e.g.
public partial class FeaturesEnum : IArrayFlags
{
public string[] Items() => new[]
{
// Name -- Value - Index - Bits
"F1", // 1 - 0 - 0001
"F2", // 2 - 1 - 0010
"F3", // 4 - 2 - 0100
"F4", // 8 - 3 - 1000
};
}
In this example, F1-F4 are the enum items that give you binary sequence values using the source generator.
Note: remember, the item's order when using IArrayFlags
is Important
after creating this class the below code will be generated in the background that you can use to work with your Enums.
public partial class FeaturesEnum
{
public const int TOTAL_ITEMS = 4;
public static readonly EnumItem None = new(0, TOTAL_ITEMS);
public static readonly EnumItem All = ~None;
public static readonly EnumItem F1 = new(1, TOTAL_ITEMS);
public static readonly EnumItem F2 = new(2, TOTAL_ITEMS);
public static readonly EnumItem F3 = new(3, TOTAL_ITEMS);
public static readonly EnumItem F4 = new(4, TOTAL_ITEMS);
}
You can use the IIndexDictionaryFlags
instead of IArrayFlags
if you wanna take control of the item's order and values.
IIndexDictionaryFlags
e.g
public partial class FeaturesEnum : IIndexDictionaryFlags
{
public Dictionary<string, int> Items() => new()
{
// Name, Order Index - Value - Bits
{ "F1", 2 }, // 2 - 4 - 100
{ "F2", 0 }, // 0 - 1 - 001
{ "F3", 1 } // 1 - 2 - 010
};
}
2. Manual
In the previous example we saw the generated code using source generator. The second way of creating Enums is to manually create this class which gives us the same functionality. but I believe it is harder to manage.
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 = FeaturesEnum.F1 | FeaturesEnum.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(FeaturesEnum.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 = FeaturesEnum.F1.SetFlag(FeaturesEnum.F3);
var key = features.ToBase64Key();
var new_features = EnumItem.FromBase64(key);
Console.WriteLine(features == new_features); // true
Support
- Don't forget to give a ⭐ on GitHub
- Share your feedback and ideas to improve this tool
- 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
Product | Versions 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 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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. 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. |
-
.NETStandard 2.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.