RSCG – Flaggen
| name | Flaggen |
| nuget | https://www.nuget.org/packages/Flaggen/ |
| link | https://github.com/ricardoboss/Flaggen |
| author | Ricardo Boss |
Explicit operations about flags with enums, and bitwise operations
This is how you can use Flaggen .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Flaggen" Version="1.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
The code that you will use is
// See https://aka.ms/new-console-template for more information
using EnumDemo;
Console.WriteLine("Hello, World!");
var color = Colors.Red ;
Console.WriteLine($"Selected Colors: {color}");
color.Add(Colors.Blue);
Console.WriteLine($"After Adding Blue: {color}");
color.Remove(Colors.Red);
Console.WriteLine($"After Removing Red: {color}");
color.Toggle(Colors.Green);
namespace EnumDemo;
[Flags]
public enum Colors
{
None = 0,
Red = 1 << 0,
Green = 1 << 1,
Blue = 1 << 2,
Yellow = 1 << 3,
Black = 1 << 4,
White = 1 << 5
}
[/code]
The code that is generated is
[code lang="csharp"]
using System;
namespace EnumDemo
{
public static class ColorsFlaggenExtensions
{
public static void Add(ref this Colors value, Colors flag)
{
value |= flag;
}
public static void Remove(ref this Colors value, Colors flag)
{
value &= ~flag;
}
public static void Toggle(ref this Colors value, Colors flag)
{
value ^= flag;
}
public static bool Has(ref this Colors value, Colors flag)
{
return (value & flag) == flag;
}
}
}
[/code]
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Flaggen