Set enumeration to default

I am sure this is pretty trivial, but I cannot get it right.

public static string DoSomething(this Enum value) { if (!Enum.IsDefined(value.GetType(), value)) { // not a valid value, assume default value value = default(value.GetType()); } // ... do some other stuff } 

String value = default(value.GetType()); not compiling, but hopefully you can see what I'm trying to do. I need to set the Enum parameter to the default of its own type.

+7
enums c # default-value value-type
source share
5 answers

I'm not quite sure what you are trying to do here, but the version of the "default" line that does the compilation is this:

  value = (Enum)Enum.GetValues(value.GetType()).GetValue(0); 

Or, even cleaner (from Paw, in the comments, thanks):

  value = (Enum) Enum.ToObject(value.GetType(), 0); 

This second version only works correctly if you know that the first element of the enumeration is 0.

+5
source share

In fact, you could do what Paw offers , even with a general limitation, if you can move this method to your class:

 public abstract class Helper<T> { public static string DoSomething<TEnum>(TEnum value) where TEnum: struct, T { if (!Enum.IsDefined(typeof(TEnum), value)) { value = default(TEnum); } // ... do some other stuff // just to get code to compile return value.ToString(); } } public class EnumHelper : Helper<Enum> { } 

Then you would do, for example:

 MyEnum x = MyEnum.SomeValue; MyEnum y = (MyEnum)100; // Let say this is undefined. EnumHelper.DoSomething(x); // generic type of MyEnum can be inferred EnumHelper.DoSomething(y); // same here 

As Conrad Rudolph notes in a comment, default(TEnum) in the code above will be evaluated to 0, regardless of whether or not a value is defined for 0 for this type of TEnum . If this is not what you want, Will respond , this is by far the easiest way to get the first specific value ( (TEnum)Enum.GetValues(typeof(TEnum)).GetValue(0) ).

On the other hand, if you want to do this with extreme and cache the result so that you do not always need to insert it, you can do this:

 public abstract class Helper<T> { static Dictionary<Type, T> s_defaults = new Dictionary<Type, T>(); public static string DoSomething<TEnum>(TEnum value) where TEnum: struct, T { if (!Enum.IsDefined(typeof(TEnum), value)) { value = GetDefault<TEnum>(); } // ... do some other stuff // just to get code to compile return value.ToString(); } public static TEnum GetDefault<TEnum>() where TEnum : struct, T { T definedDefault; if (!s_defaults.TryGetValue(typeof(TEnum), out definedDefault)) { // This is the only time you'll have to box the defined default. definedDefault = (T)Enum.GetValues(typeof(TEnum)).GetValue(0); s_defaults[typeof(TEnum)] = definedDefault; } // Every subsequent call to GetDefault on the same TEnum type // will unbox the same object. return (TEnum)definedDefault; } } 
+2
source share

Activator.CreateInstance(typeof(ConsoleColor)) works

+1
source share

Enum defaults to first value as default value

-one
source share

Do you think this is a universal method?

 public static string DoSomething<T>(Enum value) { if (!Enum.IsDefined(typeof(T), value)) { value = (T)Enum.ToObject(typeof(T), 0); } // ... do some other stuff } 

The calling method must know the type.

-one
source share

All Articles