Most efficient way to switch on a Guid in C#
c#, guid, performance
Solution
You can create a `System.Collections.Generic.Dictionary``<Guid, ...>` where `...` is something useful to you.
Upon program startup, fill the dictionary with the guids and values that you need to recognize.
Then, use the `TryGetValue` method of the dictionary to retrieve a value by its guid.
I haven't stated anything literal for `...` because I don't know what you want to do with the guids. Maybe you want to run some function, then a method pointer (`Func<T>` or something like that) might be appropriate, or otherwise an interface type that provides the method you want to invoke. This depends on the context/purpose of that guid comparing code.
Problem
So in C# the switch statement only supports integral types (not Guids), so a simple O(1) comparison table doesn't look possible. What is the most computationally efficient way to match on a Guid At first I thought ``` if(gMyGuid == new Guid("VALUE")) else if (gMyGuid == new Guid("VALUE2") else if (gMyGuid == new Guid("VALUE3") ... else if (gMyGuid == new Guid("VALUEn") ``` However by doing this I'm creating a new instance of the Guid each time for a comparison. I could convert the Guid to a string then compare on the string but the string comparison is a pretty long string for comparison. Any advise is gratefully received.