https://github.com/Spinnernicholas/EnumFastToStringDotNet
  
  
    This is a Visual Studio C# source generator for automatically generating enum extension methods that implement a switch expression based ToString method. Why? The default enum ToString method implements a binary search which is great for large lists but becomes time and memory inefficient for a just a few items when compared to a switch expression. 
    https://github.com/Spinnernicholas/EnumFastToStringDotNet
  
csharp csharp-sourcegenerator dotnet roslyn
        Last synced: 7 months ago 
        JSON representation
    
This is a Visual Studio C# source generator for automatically generating enum extension methods that implement a switch expression based ToString method. Why? The default enum ToString method implements a binary search which is great for large lists but becomes time and memory inefficient for a just a few items when compared to a switch expression.
- Host: GitHub
- URL: https://github.com/Spinnernicholas/EnumFastToStringDotNet
- Owner: Spinnernicholas
- Created: 2021-08-26T18:14:35.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-03-28T19:58:00.000Z (over 3 years ago)
- Last Synced: 2024-08-01T22:43:55.813Z (about 1 year ago)
- Topics: csharp, csharp-sourcegenerator, dotnet, roslyn
- Language: C#
- Homepage:
- Size: 28.3 KB
- Stars: 31
- Watchers: 3
- Forks: 2
- Open Issues: 1
- 
            Metadata Files:
            - Readme: README.md
 
Awesome Lists containing this project
- csharp-source-generators - EnumFastToStringDotNet -   - Automatically generates enum extension methods that implement a switch expression based ToString method. (Source Generators / Enums)
- RSCG_Examples - EnumFastToStringDotNet
README
          # EnumFastToStringDotNet
This is a Visual Studio C# source generator for automatically generating enum extension methods that implement a switch expression based ToString method. Why? The default enum ToString method implements a binary search which is great for large lists but becomes time and memory inefficient for a just a few items when compared to a switch expression. Binary search has an O(log n) while a switch expression in this case has a O(1).
You can thank [Nick Chapsas](https://github.com/Elfocrash) on YouTube for making me aware of this. Check out his awesome video here: [C#'s Enum performance trap your code is suffering from](https://www.youtube.com/watch?v=BoE5Y6Xkm6w)
## Benchmark
Benchmark done with [BenchmarkDotNet](https://benchmarkdotnet.org/). You can run it for yourself by opening the solution in Visual Studio and running the samples/Benchmark project.
```
|                         Method |      Mean |     Error |    StdDev |  Gen 0 | Allocated |
|------------------------------- |----------:|----------:|----------:|-------:|----------:|
|                 NativeToString | 25.871 ns | 0.5217 ns | 0.4625 ns | 0.0057 |      24 B |
|        SwitchStatementToString |  1.265 ns | 0.0512 ns | 0.0479 ns |      - |         - |
|       SwitchExpressionToString |  1.399 ns | 0.0565 ns | 0.0529 ns |      - |         - |
| EnumFastToStringDotNetToString |  1.510 ns | 0.0224 ns | 0.0175 ns |      - |         - |
```
## Quick Start
1. Add source generator reference to your project
```xml
  
    
  
```
2. Add using statement
```c#
using EnumFastToStringGenerated;
```
3. Add [FastToString] attribute to your enum
```c#
[FastToString]
public enum HumanStates
{
    Idle,
    Working,
    Sleeping,
    Eating,
    Dead
}
```
4. Call FastToString method
```c#
Console.Out.WriteLine(HumanStates.Dead.FastToString());
```
5. The Automatically Generated Code
```c#
using System;
namespace EnumFastToStringGenerated
{
    public static class EnumExtensions
    {
        public static string FastToString(this Namespace.HumanStates states)
        {
            return states switch
            {
                Namespace.HumanStates.Idle => nameof(Namespace.HumanStates.Idle),
                Namespace.HumanStates.Working => nameof(Namespace.HumanStates.Working),
                Namespace.HumanStates.Sleeping => nameof(Namespace.HumanStates.Sleeping),
                Namespace.HumanStates.Eating => nameof(Namespace.HumanStates.Eating),
                Namespace.HumanStates.Dead => nameof(Namespace.HumanStates.Dead),
                _ => throw new ArgumentOutOfRangeException(nameof(states), states, null)
            };
        }
    }
}
```
## Custom Method Name
If you would like to use a different method name, you can do so with the attribute argument "MethodName".
```c#
[FastToString(MethodName = "CustomMethodName")]
public enum HumanStates
{
    Idle,
    Working,
    Sleeping,
    Eating,
    Dead
}
```
Then you can call the method like this:
```c#
Console.Out.WriteLine(HumanStates.Dead.CustomMethodName());
```
## Customizing Source Generator
You can customize the source generator by changing the const values in the EnumSwitchSourceGenerator.cs file.
```c#
//The namespace that the attribute and extension method classes are defined in
private const string NAMESPACE = "EnumFastToStringGenerated";
//The name of the attribute
private const string ATTRIBUTE_NAME = "FastToString";
//The name of the extension method
private const string EXTENSION_METHOD_NAME = "FastToString";
```