{"id":13629574,"url":"https://github.com/Spinnernicholas/EnumFastToStringDotNet","last_synced_at":"2025-04-17T09:34:50.430Z","repository":{"id":40723427,"uuid":"400264527","full_name":"Spinnernicholas/EnumFastToStringDotNet","owner":"Spinnernicholas","description":"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.","archived":false,"fork":false,"pushed_at":"2022-03-28T19:58:00.000Z","size":29,"stargazers_count":31,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-08-01T22:43:55.813Z","etag":null,"topics":["csharp","csharp-sourcegenerator","dotnet","roslyn"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Spinnernicholas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-26T18:14:35.000Z","updated_at":"2024-04-08T10:52:07.000Z","dependencies_parsed_at":"2022-08-19T01:21:10.250Z","dependency_job_id":null,"html_url":"https://github.com/Spinnernicholas/EnumFastToStringDotNet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Spinnernicholas%2FEnumFastToStringDotNet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Spinnernicholas%2FEnumFastToStringDotNet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Spinnernicholas%2FEnumFastToStringDotNet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Spinnernicholas%2FEnumFastToStringDotNet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Spinnernicholas","download_url":"https://codeload.github.com/Spinnernicholas/EnumFastToStringDotNet/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223751241,"owners_count":17196593,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","csharp-sourcegenerator","dotnet","roslyn"],"created_at":"2024-08-01T22:01:13.926Z","updated_at":"2024-11-08T20:31:15.761Z","avatar_url":"https://github.com/Spinnernicholas.png","language":"C#","readme":"# EnumFastToStringDotNet\n\nThis 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).\n\nYou 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)\n\n## Benchmark\nBenchmark 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.\n```\n|                         Method |      Mean |     Error |    StdDev |  Gen 0 | Allocated |\n|------------------------------- |----------:|----------:|----------:|-------:|----------:|\n|                 NativeToString | 25.871 ns | 0.5217 ns | 0.4625 ns | 0.0057 |      24 B |\n|        SwitchStatementToString |  1.265 ns | 0.0512 ns | 0.0479 ns |      - |         - |\n|       SwitchExpressionToString |  1.399 ns | 0.0565 ns | 0.0529 ns |      - |         - |\n| EnumFastToStringDotNetToString |  1.510 ns | 0.0224 ns | 0.0175 ns |      - |         - |\n```\n\n## Quick Start\n\n1. Add source generator reference to your project\n```xml\n  \u003cItemGroup\u003e\n    \u003cProjectReference Include=\"path\\to\\EnumFastToStringDotNet.csproj\" OutputItemType=\"Analyzer\" ReferenceOutputAssembly=\"false\" /\u003e\n  \u003c/ItemGroup\u003e\n```\n2. Add using statement\n```c#\nusing EnumFastToStringGenerated;\n```\n3. Add [FastToString] attribute to your enum\n```c#\n[FastToString]\npublic enum HumanStates\n{\n    Idle,\n    Working,\n    Sleeping,\n    Eating,\n    Dead\n}\n```\n4. Call FastToString method\n```c#\nConsole.Out.WriteLine(HumanStates.Dead.FastToString());\n```\n\n5. The Automatically Generated Code\n```c#\nusing System;\nnamespace EnumFastToStringGenerated\n{\n    public static class EnumExtensions\n    {\n        public static string FastToString(this Namespace.HumanStates states)\n        {\n            return states switch\n            {\n                Namespace.HumanStates.Idle =\u003e nameof(Namespace.HumanStates.Idle),\n                Namespace.HumanStates.Working =\u003e nameof(Namespace.HumanStates.Working),\n                Namespace.HumanStates.Sleeping =\u003e nameof(Namespace.HumanStates.Sleeping),\n                Namespace.HumanStates.Eating =\u003e nameof(Namespace.HumanStates.Eating),\n                Namespace.HumanStates.Dead =\u003e nameof(Namespace.HumanStates.Dead),\n                _ =\u003e throw new ArgumentOutOfRangeException(nameof(states), states, null)\n            };\n        }\n    }\n}\n```\n\n## Custom Method Name\n\nIf you would like to use a different method name, you can do so with the attribute argument \"MethodName\".\n```c#\n[FastToString(MethodName = \"CustomMethodName\")]\npublic enum HumanStates\n{\n    Idle,\n    Working,\n    Sleeping,\n    Eating,\n    Dead\n}\n```\nThen you can call the method like this:\n```c#\nConsole.Out.WriteLine(HumanStates.Dead.CustomMethodName());\n```\n\n## Customizing Source Generator\n\nYou can customize the source generator by changing the const values in the EnumSwitchSourceGenerator.cs file.\n```c#\n//The namespace that the attribute and extension method classes are defined in\nprivate const string NAMESPACE = \"EnumFastToStringGenerated\";\n\n//The name of the attribute\nprivate const string ATTRIBUTE_NAME = \"FastToString\";\n\n//The name of the extension method\nprivate const string EXTENSION_METHOD_NAME = \"FastToString\";\n```\n","funding_links":[],"categories":["Source Generators","Do not want to test 112 ( old ISourceGenerator )"],"sub_categories":["Enums","1. [ThisAssembly](https://ignatandrei.github.io/RSCG_Examples/v2/docs/ThisAssembly) , in the [EnhancementProject](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#enhancementproject) category"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSpinnernicholas%2FEnumFastToStringDotNet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSpinnernicholas%2FEnumFastToStringDotNet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSpinnernicholas%2FEnumFastToStringDotNet/lists"}