I have this Enum code:
enum Duration { Day, Week, Month };
Can I add a extension methods for this Enum?
You can also add an extension method to the Enum type rather than an instance of the Enum:
/// Enum Extension Methods
/// type of Enum
public class Enum where T : struct, IConvertible
{
public static int Count
{
get
{
if (!typeof(T).IsEnum)
throw new ArgumentException("T must be an enumerated type");
return Enum.GetNames(typeof(T)).Length;
}
}
}
You can invoke the extension method above by doing:
var result = Enum.Count;
It's not a true extension method. It only works because Enum<> is a different type than System.Enum.