Skip to content

Instantly share code, notes, and snippets.

@cybermaxs
Created October 13, 2016 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cybermaxs/2a580d35be4232e2470eb947995e7d7a to your computer and use it in GitHub Desktop.
Save cybermaxs/2a580d35be4232e2470eb947995e7d7a to your computer and use it in GitHub Desktop.
Cache Enum.GetValues in an efficient way (I think :))
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