CodeChops.MagicEnums
3.7.1
See the version list below for details.
dotnet add package CodeChops.MagicEnums --version 3.7.1
NuGet\Install-Package CodeChops.MagicEnums -Version 3.7.1
<PackageReference Include="CodeChops.MagicEnums" Version="3.7.1" />
paket add CodeChops.MagicEnums --version 3.7.1
#r "nuget: CodeChops.MagicEnums, 3.7.1"
// Install CodeChops.MagicEnums as a Cake Addin #addin nuget:?package=CodeChops.MagicEnums&version=3.7.1 // Install CodeChops.MagicEnums as a Cake Tool #tool nuget:?package=CodeChops.MagicEnums&version=3.7.1
Magic Enums
Customizable, and extendable enums for C# with a clean API. See advantages.
Check out CodeChops projects for more projects.
Basic examples
Simple enum
/* This is a simple example of how to create an int enum with explicit and implicit values. */
public record Level : MagicEnum<Level>
{
public static readonly Level Low = CreateMember(); // 0
public static readonly Level Medium = CreateMember(1); // 1
public static readonly Level High = CreateMember(); // 2
}
Extended enum
/* This is an example of enum mebers with extra properties. */
public record Vehicle(int WheelCount) : MagicEnum<Vehicle>
{
public static readonly Type.Bicycle Bicycle = CreateMember<Type.Bicycle>();
public static readonly Type.MotorCycle MotorCycle = CreateMember<Type.MotorCycle>();
public static readonly Type.Car FuelCar = CreateMember<Type.Car>(() => new(EmitsCo2: true));
public static readonly Type.Car ElectricCar = CreateMember<Type.Car>(() => new(EmitsCo2: false));
public static class Type
{
public record Bicycle() : Vehicle(WheelCount: 2);
public record MotorCycle() : Vehicle(WheelCount: 2);
public record Car(bool EmitsCo2) : Vehicle(WheelCount: 4);
}
}
Another usage example in the Currency.cs file of the FoodChops application.
Functionality
Terminology used in this documentation:
- An
enum
has one or multiple members.- Each
member
has aname
and avalue
.- A member
name
has to be unique across all members of an enum.- A member
value
does not have to be unique across all members.- The type of the value depends on the chosen
enum type
, see: enum types.
Magic enums behave like the default .NET enum implementation:
- They use
int
as default for the member value. - Members can be found by searching for their name or value.
- More than one member name can be assigned to the same value but only one value can be assigned to a member name.
- The value of members can be omitted (when using the default numeric enums). If omitted, it automatically increments the value of each member.
- Members, member names or member values can easily be enumerated.
Flag
enums are supported.- Strongly typed enum members, so pattern matching can be used.
Advantages
Besides the default .NET enum behaviour, MagicEnums offer more features than the default .NET enum implementation:
- Fast and optimized: does not use reflection.
- Extendability:
- Inheritance is supported. This way enums can also be extended in other assemblies.
- Partial enums are supported.
- Custom methods and properties can be added to the enums.
- Different types of enums are supported:
- Number enums (including
decimal
) - Flags enums
- String enums
- Custom enums
- Number enums (including
- Members can be added at runtime, if necessary. This is thread-safe.
- Members with the same value can be looked up easily. Something which is not supported in default C# enums.
- Optimized, and therefore fast member registration and lookup, including a fast
ToString
. For extra optimization, see optimization. - Serialization to/from JSON is supported.
- Members can be auto-discovered. This removes the need to keep track of used/unused enum-members. See auto member discoverability.
Usage
- Add the package
CodeChops.MagicEnums
. - Add a (global) using to namespace
CodeChops.MagicEnums
. - Create a record which implements one of the enum types.
- Add members:
- By creating
static readonly
members that callCreateMember()
. - And/or use member generation.
- By creating
API
Method | Description |
---|---|
CreateMember * |
Creates a new enum member and returns it. |
GetEnumerator |
Gets an enumerator over the enum members. |
GetMembers |
Gets an enumerable over:<br/>- All enum members, or<br/>- Members of a specific value: Throws when no member has been found. |
GetValues |
Gets an enumerable over the member values. |
TryGetMembers |
Tries to get member(s) by value. |
TryGetSingleMember |
Tries to get a single member by name / value.<br/>Throws when multiple members of the same value have been found. |
GetSingleMember |
Gets a single member by name / value.<br/>Throws when not found or multiple members have been found. |
GetUniqueValueCount |
Gets the unique member value count. |
GetMemberCount |
Gets the member count. |
GetDefaultValue |
Gets the default value of the enum. |
GetOrCreateMember * |
Creates a member or gets one if a member already exists. |
The methods are
public
, except for the ones marked with *, they areprotected
.
Enum types
Number enums
Number enums (default) have a numeric type as value.
- Can be created by implementing
MagicEnum<TSelf, TNumber>
. - If
TNumber
is omitted,int
will be used as type:MagicEnum<TSelf>
. TNumber
can be of any type that are also supported by the default .NET implementation:byte
,sbyte
,short
,ushort
,int
,uint
,long
, orulong
.- Unlike the default C# .NET implementation,
decimal
is also supported. - Implicit and explicit value declaration are supported, see the example below.
/* This example shows an int enum with implicit and explicit values. */
public record StarRating : MagicEnum<StarRating>
{
public static readonly StarRating One = CreateMember(1);
public static readonly StarRating Two = CreateMember();
public static readonly StarRating Three = CreateMember();
public static readonly StarRating Four = CreateMember();
public static readonly StarRating Five = CreateMember();
}
The example creates an enum with an int
as member value.
The value of the first member is explicitly defined.
Other values are being incremented automatically, because they are defined implicitly.
Example
/* This example shows the usage of a `decimal` as member value. */
public record EurUsdRate : MagicEnum<EurUsdRate, decimal>
{
public static readonly EurUsdRate Average2021 = CreateMember(0.846m);
public static readonly EurUsdRate Average2020 = CreateMember(0.877m);
public static readonly EurUsdRate Average2019 = CreateMember(0.893m);
public static readonly EurUsdRate Average2018 = CreateMember(0.848m);
}
Flags enums
Flag enums are supported just like in the default .NET implementation. Implicit value declaration is also support. See the example below. Flags enums offer extra methods:
GetUniqueFlags()
: Gets the unique flags of the provided value.HasFlag()
: Returnstrue
if a specific enum member contains the provided flag.
Example
/* This example shows the usage of a flags enum.
Note that member 'ReadAndWrite' flags both 'Read' and 'Write'. */
public record Permission : MagicFlagsEnum<Permission>
{
public static readonly Permission None = CreateMember(); // 0
public static readonly Permission Read = CreateMember(); // 1 << 0
public static readonly Permission Write = CreateMember(); // 1 << 1
public static readonly Permission ReadAndWrite = CreateMember(Read | Write);
}
String enums
Sometimes you only need an enumeration of strings
(for example: names). In this case the underlying numeric value is not important. Magic string enums helps you achieving this:
- Can be created by implementing
MagicStringEnum<TSelf>
. - Ensure that the values of the members are equal to the name of the members. This can be manually overriden, if necessary.
- Prohibit incorrect usage of numeric values when they are not needed.
- Remove the need to keep track of (incremental) numeric values.
- When members are generated, they show an automatic warning in the comments that the members shouldn't be renamed. See member generation.
Example
/* This example shows the creation of a string enum.
The value of the members are equal to the name of the members. */
using CodeChops.MagicEnums;
public record ErrorCode : MagicStringEnum<ErrorCode>
{
public static readonly ErrorCode EndpointDoesNotExist = CreateMember();
public static readonly ErrorCode InvalidParameters = CreateMember();
public static readonly ErrorCode NotAuthorized = CreateMember();
}
Custom enums
Custom enums can also be created. They offer a way to create an enum of any type that you prefer:
- Can be created by implementing
MagicCustomEnum<TSelf, TValue>
. TValue
should implementIEquatable
andIComparable
.- A custom value type can easily be generated using the Value object generator which is included in the Domain Modeling-library.
- Two dogfooding examples of the usage custom enums:
- See Strict direction modes in the Geometry library. It contains enums that have a
2D-point
as member value. - See Implementation discovery, which automatically creates an enum of every implementation of a specific
base class
/interface
. Each member contains its (uninitialized) instance as value.
- See Strict direction modes in the Geometry library. It contains enums that have a
Pattern matching
To achieve pattern matching, you can do the following below.
var message = level.Name switch
{
nameof(Level.Low) => "The level is low.",
nameof(Level.Medium) => "The level is medium.",
nameof(Level.High) => "The level is high.",
_ => throw new UnreachableException($"This should not occur.")
};
In this example, the enum from the default usage example is used.
Another way is to define the types in an inner class and use them as the type of an enum member:
var speedingFineInEur = vehicle switch
{
Vehicle.Type.MotorCycle => 60,
Vehicle.Type.Car => 100,
_ => 0,
};
In this example, the enum from the extended usage example is used.
Member generation
Aside from defining members using CreateMember
, members can be generated automatically using C# source generators.
Attributes
Members can be generated using the EnumMember
attribute. A value
and a comment can be provided. This method uses a different syntax than the default CreateMember
syntax and offers no advantages over it.
Example
[EnumMember("Low")]
[EnumMember("Medium", 1)]
[EnumMember("High", comment: "Has a value of 2.")]
public partial record Level : MagicEnum<Level>;
The following code will be generated:
// <auto-generated />
#nullable enable
using System;
using System;
using CodeChops.MagicEnums;
namespace Test;
/// <summary>
/// <list type="bullet">
/// <item><c><![CDATA[ Low = 0 ]]></c></item>
/// <item><c><![CDATA[ Medium = 1 ]]></c></item>
/// <item><c><![CDATA[ High = 2 ]]></c></item>
/// </list>
/// </summary>
public partial record class Level
{
/// <summary>
/// <c><![CDATA[ (value: 0) ]]></c>
/// </summary>
public static Level Low { get; } = CreateMember(0);
/// <summary>
/// <c><![CDATA[ (value: 1) ]]></c>
/// </summary>
public static Level Medium { get; } = CreateMember(1);
/// <summary>
/// <para>Has a value of 2.</para>
/// <c><![CDATA[ (value: 2) ]]></c>
/// </summary>
public static Level High { get; } = CreateMember(2);
}
#nullable restore
Member discoverability
Enum member discoverability makes it possible to automatically generate an enum member the moment you reference it in your IDE. This ensures that enum members that are not used anymore are deleted.
Explicit discoverability
- Is enabled by adding the
DiscoverEnumMembers
-attribute on the enum. - Use [Enum].[Member].CreateMember() to create a source generated enum-member on the fly.
- A member value and/or comment can be provided.
Example without arguments
Example with arguments
It generates the following code:
// <auto-generated />
#nullable enable
using System;
using System;
using CodeChops.MagicEnums;
namespace Test;
/// <summary>
/// <list type="bullet">
/// <item><c><![CDATA[ CreditCard = 7 ]]></c></item>
/// </list>
/// </summary>
public sealed partial record class PaymentMethod
{
/// <summary>
/// <para>Only works with VISA.</para>
/// <c><![CDATA[ (value: 7) ]]></c>
/// </summary>
public static PaymentMethod CreditCard { get; } = CreateMember(7);
}
/// <summary>
/// Call this method in order to create discovered enum members while invoking them (on the fly). So enum members are automatically deleted when not being used.
/// </summary>
public static class PaymentMethodExtensions
{
public static PaymentMethod CreateMember(this PaymentMethod member, Int32? value = null, string? comment = null)
=> member;
}
#nullable restore
Implicit discoverability
- Is enabled by adding the
DiscoverEnumMembers
-attribute withimplicit
set totrue
. - Use [Enum].[Member] to create a source generated enum-member on the fly.
- No member value or comment can be provided.
Example
It generates the following code:
// <auto-generated />
#nullable enable
using System;
using System;
using CodeChops.MagicEnums;
namespace Test;
/// <summary>
/// <para><em>Do not rename!</em></para>
/// <list type="bullet">
/// <item><c><![CDATA[ NameIsNullOrWhiteSpace = "NameIsNullOrWhiteSpace" ]]></c></item>
/// </list>
/// </summary>
public sealed partial record class ErrorCode
{
/// <summary>
/// <para><em>Do not rename!</em></para>
/// <para><c><![CDATA[ (value: "NameIsNullOrWhiteSpace") ]]></c></para>
/// </summary>
public static ErrorCode NameIsNullOrWhiteSpace { get; } = CreateMember("NameIsNullOrWhiteSpace");
}
/// <summary>
/// Call this method in order to create discovered enum members while invoking them (on the fly). So enum members are automatically deleted when not being used.
/// </summary>
public static class ErrorCodeExtensions
{
public static ErrorCode CreateMember(this ErrorCode member, String? value = null, string? comment = null)
=> member;
}
#nullable restore
Optimization
Generally your enum does not dynamically add members at runtime. If this is the case, the attribute DisableConcurrency
can be placed on the enum. It disables concurrency and therefore optimises memory usage and speed.
Warning! Only use this attribute when you are sure that no race conditions can take place when creating / reading members.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
-
net7.0
- CodeChops.DomainModeling (>= 2.6.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on CodeChops.MagicEnums:
Package | Downloads |
---|---|
CodeChops.ImplementationDiscovery
Provides easy-accessible, design-time and runtime information about implementations throughout your code. |
|
CodeChops.MagicEnums.Json
Json (de)serialization for MagicEnums. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.9.2 | 413 | 10/16/2024 |
3.9.1 | 215 | 9/29/2024 |
3.9.0 | 639 | 3/20/2023 |
3.8.3 | 429 | 3/10/2023 |
3.8.2 | 448 | 3/6/2023 |
3.8.0 | 416 | 1/27/2023 |
3.7.1 | 408 | 1/22/2023 |
3.6.0 | 859 | 1/7/2023 |
3.5.0 | 416 | 1/6/2023 |
3.4.4 | 529 | 1/6/2023 |
3.4.2 | 369 | 1/4/2023 |
3.4.1 | 458 | 1/3/2023 |
3.4.0 | 383 | 1/2/2023 |
3.3.9 | 332 | 1/2/2023 |
3.3.5 | 397 | 12/23/2022 |
3.3.4 | 347 | 12/22/2022 |
3.3.3 | 323 | 12/19/2022 |
3.3.2 | 365 | 12/16/2022 |
3.3.1 | 360 | 12/15/2022 |
3.3.0 | 339 | 12/14/2022 |
2.9.9 | 592 | 9/17/2022 |
2.9.8 | 576 | 9/16/2022 |
2.9.5 | 591 | 9/16/2022 |
2.9.4 | 469 | 9/15/2022 |
2.9.3 | 487 | 9/14/2022 |
1.5.1 | 586 | 7/11/2022 |
1.5.0 | 758 | 7/11/2022 |
1.4.3 | 641 | 7/11/2022 |
1.4.2 | 1,221 | 7/10/2022 |
1.4.1 | 838 | 7/8/2022 |
1.3.10 | 593 | 7/8/2022 |
1.2.9 | 912 | 7/6/2022 |
1.2.8 | 457 | 7/6/2022 |
1.2.7 | 456 | 7/5/2022 |
1.2.6 | 464 | 7/5/2022 |
1.2.5 | 1,581 | 6/23/2022 |
1.2.4 | 449 | 6/22/2022 |
1.2.3 | 697 | 6/21/2022 |
1.2.2 | 487 | 6/14/2022 |
1.2.1 | 2,163 | 6/14/2022 |
1.2.0 | 1,355 | 6/13/2022 |
1.1.1 | 1,850 | 3/16/2022 |
1.1.0 | 478 | 3/9/2022 |
1.0.3 | 611 | 3/4/2022 |
1.0.2 | 472 | 3/3/2022 |
1.0.1 | 471 | 3/3/2022 |
0.9.6 | 481 | 2/20/2022 |
Updated package references 2.