InfiniteEnumFlags 0.0.3
See the version list below for details.
dotnet add package InfiniteEnumFlags --version 0.0.3
NuGet\Install-Package InfiniteEnumFlags -Version 0.0.3
<PackageReference Include="InfiniteEnumFlags" Version="0.0.3" />
paket add InfiniteEnumFlags --version 0.0.3
#r "nuget: InfiniteEnumFlags, 0.0.3"
// Install InfiniteEnumFlags as a Cake Addin #addin nuget:?package=InfiniteEnumFlags&version=0.0.3 // Install InfiniteEnumFlags as a Cake Tool #tool nuget:?package=InfiniteEnumFlags&version=0.0.3
InfiniteEnumFlags
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 YourCustomEnumName : 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 YourCustomEnumName
{
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 TestIndexDictionaryFlags : 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 = YourCustomEnumName.F1 | YourCustomEnumName.F3; // (+) F1 + F3
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.