Skip to content

Instantly share code, notes, and snippets.

@jessestricker
Last active April 20, 2016 19:42
Show Gist options
  • Save jessestricker/8d0b9cf85f759d79b03b to your computer and use it in GitHub Desktop.
Save jessestricker/8d0b9cf85f759d79b03b to your computer and use it in GitHub Desktop.
[C#] A generic util class for enums.
using System;
using System.Collections.Generic;
using System.Linq;
public static class Enum<T> where T : struct
{
public static IEnumerable<EnumValue<T>> Values =>
from object value in Enum.GetValues(typeof(T))
select new EnumValue<T>((T)value);
public static IEnumerable<string> Names =>
Enum.GetNames(typeof(T));
public static string GetName(T value) =>
Enum.GetName(typeof(T), value);
}
public class EnumValue<T> where T : struct
{
public EnumValue(T value)
{
Value = value;
}
public T Value { get; }
public string Name => Enum<T>.GetName(Value);
public static implicit operator T(EnumValue<T> v) => v.Value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment