Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author contact: piecesof8bitgame@gmail.com
- /// <summary>
- /// Helper to create and cache an array of all values of the specified enum type
- /// Avoids constant garbage allocation from evaluating Enum.GetValues every time we want all values
- /// </summary>
- /// <typeparam name="TEnum">Type of an enum to use</typeparam>
- public static class EnumValues<TEnum>
- where TEnum : struct, IComparable, IConvertible, IFormattable
- {
- private static TEnum[] GetValues()
- {
- var values = Enum.GetValues(typeof(TEnum));
- var array = new TEnum[values.Length];
- for (var i = 0; i < values.Length; i++)
- {
- array[i] = (TEnum) values.GetValue(i);
- }
- return array;
- }
- // NB: .NET 4.6 only; use a property with a private readonly backing field for .NET 3.5
- public static TEnum[] Values { get; } = GetValues();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement