Lookups

The Intellenum library provides a fast and efficient way to work with enums. It uses source generation to generate backing code for extremely fast, and allocation-free, lookups using the FromName and FromValue methods (and the equivalent Try... methods). README.md

How Lookups Work

Intellenum leverages the switch statement’s ability to handle constant expressions. This allows for highly optimized lookups, but it places restrictions on the underlying data types.

Supported Data Types

The following data types are supported for compile-time constant patterns within the switch statement:

  • byte (and unsigned byte)
  • int16 (and unsigned int16)
  • int32 (and unsigned int32)
  • int64 (and unsigned int64)
  • string
  • decimal README.md

Lookup Tables for Other Types

For underlying types that are not one of the supported compile-time constants, a lookup table is generated. This is necessary because the left-hand side of a switch expression must be a constant expression. README.md

Lookup Examples

Example: FromName and FromValue

var find1 = CustomerType.FromName("Gold");
          var find2 = CustomerType.FromValue(2);
          ``` [docs/site/Writerside/topics/tutorials/your-first-intellenum-enum.md](https://github.com/stevedunn/intellenum/blob/main/docs/site/Writerside/topics/tutorials/your-first-intellenum-enum.md)
          
          **Example: `TryFromName` and `TryFromValue`**
          
          ```c#
          bool b1 = CustomerType.TryFromName("Silver", out var ct1);
          bool b2 = CustomerType.TryFromValue(3, out var ct2);
          ``` [docs/site/Writerside/topics/discussions/Overview.md](https://github.com/stevedunn/intellenum/blob/main/docs/site/Writerside/topics/discussions/Overview.md)
          
          ### Additional Features
          
          **Listing Values**
          
          ```c#
          Console.WriteLine(
          string.Join(", ", CustomerType.List())); // Standard, Gold
          ``` [docs/site/Writerside/topics/tutorials/your-first-intellenum-enum.md](https://github.com/stevedunn/intellenum/blob/main/docs/site/Writerside/topics/tutorials/your-first-intellenum-enum.md)
          
          **Checking for Defined Values**
          
          ```c#
          Console.WriteLine(CustomerType.IsNamedDefined("Silver")); // False
          Console.WriteLine(CustomerType.IsDefined(3)); // False
          ``` [docs/site/Writerside/topics/tutorials/your-first-intellenum-enum.md](https://github.com/stevedunn/intellenum/blob/main/docs/site/Writerside/topics/tutorials/your-first-intellenum-enum.md)
          
          **Generated `TryParse` Method**
          
          If the underlying type contains a static `TryParse` method, a `TryParse` method is generated for the enum itself. This method leverages the underlying type's `TryParse` method and then performs a lookup with `TryFromValue`. [docs/site/Writerside/topics/discussions/Overview.md](https://github.com/stevedunn/intellenum/blob/main/docs/site/Writerside/topics/discussions/Overview.md)
          ```markdown
          
          
          ## 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.CodeFixers/" href="#src/Intellenum.CodeFixers/">src/Intellenum.CodeFixers/</a> - Contains code fixers 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/Shared/" href="#tests/Shared/">tests/Shared/</a> - Contains shared test files.
          
          <a class='local-link directory-link' data-ref="tests/SnapshotTests/" href="#tests/SnapshotTests/">tests/SnapshotTests/</a> - Contains snapshot tests for the Intellenum library.