Enum Enhancements

Intellenum is an open-source C# project that provides a fast and efficient way to deal with enums. It uses source generation that generates backing code for extremely fast, and allocation-free, lookups with the FromName and FromValue methods (and the equivalent Try... methods).

Example:

[Intellenum]
          public partial class CustomerType
          {
              public static readonly CustomerType Standard = new(1);
              public static readonly CustomerType Gold = new(2);
          }
          

In addition to speed, it also provides code analyzers for safety. Intellenum provides speed benefits over standard enums for when you need to see if an enum has a member of a particular name or value.

Here are some of the key features of Intellenum:

1. Fast Lookups:

Intellenum uses source generation to provide fast lookups for enum names and values, making it significantly faster than traditional enums.

Example:

// FromName
          var goldCustomer = CustomerType.FromName("Gold");
          
          // FromValue
          var standardCustomer = CustomerType.FromValue(1);
          

2. Type Safety:

Intellenum incorporates code analyzers to ensure type safety and prevent defaulting issues, making your code more robust and reliable.

Example:

// Example of an error detected by Intellenum's code analyzer
          [Intellenum]
          public partial class CustomerType
          {
              // Error: Missing a member definition
          }
          

3. ToString() Method:

The ToString() method returns the name of the enum member.

Example:

Console.WriteLine(CustomerType.Gold); // Output: Gold
          

4. Deconstruct Method:

The Deconstruct method allows you to easily access both the name and value of an enum member.

Example:

var (name, value) = CustomerType.Gold;
          Console.WriteLine(name); // Output: Gold
          Console.WriteLine(value); // Output: 2
          

5. GetMembers() Method:

The GetMembers() method returns an IEnumerable of all the members of the enum.

Example:

foreach (var member in CustomerType.GetMembers())
          {
              Console.WriteLine(member); // Output: Standard, Gold
          }
          

6. IComparable Support:

If the underlying type of an enum implements IComparable, the generated enum code will also implement IComparable. This allows for comparison operations between enum members.

Example:

public partial class PlanetEnum
          {
              public static readonly PlanetEnum Jupiter =
              new(new Planet("Brown", 273_400));
          
              public static readonly PlanetEnum Mars=
              new(new Planet("Red", 13_240));
          
              public static readonly PlanetEnum Venus=
              new(new Planet("White", 23_622));
          }
          
          public record class Planet(string Colour, int CircumferenceInMiles)
          : IComparable
          {
              public int CompareTo(Planet other) =>
              CircumferenceInMiles.CompareTo(other.CircumferenceInMiles);
          }
          

7. TryParse Method:

If the underlying type of an enum has a TryParse method, the generated enum will also have a TryParse method that delegates to the underlying type’s TryParse method. This allows for conversion of the underlying type back to a matching enum.

Example:

[Intellenum(typeof(Planet))]
          public partial class PlanetEnum
          {
              public static readonly PlanetEnum Jupiter =
              new(new Planet("Brown", 273_400));
          }
          

8. Custom Enum Member Declaration:

You can declare members in different ways, including static constructors.

Example:

[Intellenum]
          public partial class CustomerType
          {
              static CustomerType()
              {
                  Member("Standard", 1);
                  Member("Gold", 2);
              }
          }
          

9. Performance Comparison:

Intellenum offers significant performance improvements compared to standard enums and other enum libraries, as evidenced by the benchmarks provided in the repository.

References:


          ## Top-Level Directory Explanations
          
          <a class='local-link directory-link' data-ref="samples/" href="#samples/">samples/</a> - This directory contains example projects demonstrating the usage of Intellenum.
          
          <a class='local-link directory-link' data-ref="samples/Intellenum.Examples/" href="#samples/Intellenum.Examples/">samples/Intellenum.Examples/</a> - Contains various example projects demonstrating different aspects of Intellenum, such as serialization, conversion, syntax examples, types, typical scenarios, and more.
          
          <a class='local-link directory-link' data-ref="samples/WebApplication/" href="#samples/WebApplication/">samples/WebApplication/</a> - Contains a sample web application that uses Intellenum.
          
          <a class='local-link directory-link' data-ref="src/" href="#src/">src/</a> - This directory contains the source code of the Intellenum library.
          
          <a class='local-link directory-link' data-ref="src/Benchmarks/" href="#src/Benchmarks/">src/Benchmarks/</a> - Contains benchmark tests for the Intellenum library.
          
          <a class='local-link directory-link' data-ref="src/Intellenum.SharedTypes/" href="#src/Intellenum.SharedTypes/">src/Intellenum.SharedTypes/</a> - Contains shared types used across the Intellenum library.
          
          <a class='local-link directory-link' data-ref="src/Intellenum/" href="#src/Intellenum/">src/Intellenum/</a> - Contains the main source code for the Intellenum library. This directory is further divided into subdirectories for diagnostics, extensions, generators, member building, properties, rules, static constructor building, templates, and more.
          
          <a class='local-link directory-link' data-ref="tests/" href="#tests/">tests/</a> - This directory contains test projects for the Intellenum library.
          
          <a class='local-link directory-link' data-ref="tests/AnalyzerTests/" href="#tests/AnalyzerTests/">tests/AnalyzerTests/</a> - Contains unit tests for the Intellenum analyzer.
          
          <a class='local-link directory-link' data-ref="tests/ConsumerTests/" href="#tests/ConsumerTests/">tests/ConsumerTests/</a> - Contains tests for consuming the Intellenum library.
          
          <a class='local-link directory-link' data-ref="tests/Intellenum.Tests/" href="#tests/Intellenum.Tests/">tests/Intellenum.Tests/</a> - Contains additional tests for the Intellenum library.
          
          <a class='local-link directory-link' data-ref="tests/SnapshotTests/" href="#tests/SnapshotTests/">tests/SnapshotTests/</a> - Contains snapshot tests for the Intellenum library.