Created
October 13, 2016 14:09
-
-
Save cybermaxs/2a580d35be4232e2470eb947995e7d7a to your computer and use it in GitHub Desktop.
Cache Enum.GetValues in an efficient way (I think :))
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
namespace MyFunnyProject | |
{ | |
internal static class EnumHelper | |
{ | |
public static class EnumCache<TEnum> | |
{ | |
public readonly static TEnum[] Values = null; | |
static EnumCache() | |
{ | |
Values = Enum.GetValues(typeof(TEnum)).Cast<TEnum>().ToArray(); | |
} | |
} | |
public static TEnum[] AllValues<TEnum>() where TEnum : struct, IComparable, IFormattable, IConvertible | |
{ | |
return EnumCache<TEnum>.Values; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment