C # enum contains value

I have an enumeration

enum myEnum2 { ab, st, top, under, below} 

I would like to write a function to check if a given value is included in myEnum

something like that:

 private bool EnumContainValue(Enum myEnum, string myValue) { return Enum.GetValues(typeof(myEnum)) .ToString().ToUpper().Contains(myValue.ToUpper()); } 

But this does not work, because the myEnum parameter is not recognized.

+46
contains enums c #
Nov 06 '12 at 10:16
source share
10 answers

No need to write yourself:

  // Summary: // Returns an indication whether a constant with a specified value exists in // a specified enumeration. // // Parameters: // enumType: // An enumeration type. // // value: // The value or name of a constant in enumType. // // Returns: // true if a constant in enumType has a value equal to value; otherwise, false. public static bool IsDefined(Type enumType, object value); 

Example:

 if (System.Enum.IsDefined(MyEnumType, MyValue)) { // Do something } 
+32
Nov 06
source share
— -

Why not use

 Enum.IsDefined(typeof(myEnum), value); 

I like to create a generic Enum<T> class that bypasses Enum calls (in fact, I wonder why something like this was not added in Framework 2.0 or later):

 public static class Enum<T> { public static bool IsDefined(string name) { return Enum.IsDefined(typeof(T), name); } public static bool IsDefined(T value) { return Enum.IsDefined(typeof(T), value); } public static IEnumerable<T> GetValues() { return Enum.GetValues(typeof(T)).Cast<T>(); } // etc } 

This avoids all this typeof material and uses strongly typed values:

 Enum<StringSplitOptions>.IsDefined("None") 
+77
Nov 06
source share

just use this method

Enum.IsDefined Method - Returns an indication of whether a constant exists with the specified value in the specified enumeration

Example

 enum myEnum2 { ab, st, top, under, below}; myEnum2 value = myEnum2.ab; Console.WriteLine("{0:D} Exists: {1}", value, myEnum2.IsDefined(typeof(myEnum2), value)); 
+7
Nov 06 '12 at 10:20
source share

What do you do with ToString () in this case:

Enum.GetValues(typeof(myEnum)).ToString()... Instead you should write:

 Enum.GetValues(typeof(myEnum).ToString()... 

The difference in parentheses ...

+3
Nov 06 '12 at 17:45
source share
  public static T ConvertToEnum<T>(this string value) { if (typeof(T).BaseType != typeof(Enum)) { throw new InvalidCastException("The specified object is not an enum."); } if (Enum.IsDefined(typeof(T), value.ToUpper()) == false) { throw new InvalidCastException("The parameter value doesn't exist in the specified enum."); } return (T)Enum.Parse(typeof(T), value.ToUpper()); } 
+2
Nov 06
source share

Use the correct enumeration name ( myEnum2 ).

Also, if you are testing a string value, you can use GetNames instead of GetValues .

+1
Nov 06
source share

just enter the listing as:

 string something = (string)myEnum; 

and now comparison is easy as you like

+1
Nov 06 '12 at 10:22
source share

I think you are wrong when using ToString ().

Try a Linq query

 private bool EnumContainValue(Enum myEnum, string myValue) { var query = from enumVal in Enum.GetNames(typeof(GM)).ToList() where enumVal == myValue select enumVal; return query.Count() == 1; } 
+1
Nov 06 '12 at 10:25
source share

You can also use this:

  enum myEnum2 { ab, st, top, under, below } static void Main(string[] args) { myEnum2 r; string name = "ab"; bool result = Enum.TryParse(name, out r); } 

The result will indicate whether the value is listed or not.

+1
Nov 06
source share

If your question is similar to "I have an enum MyEnum { OneEnumMember, OtherEnumMember } type, enum MyEnum { OneEnumMember, OtherEnumMember } , and I would like to have a function that reports whether this enumeration type contains a member with a specific name, what you are looking for is the System.Enum.IsDefined method System.Enum.IsDefined :

 Enum.IsDefined(typeof(MyEnum), MyEnum.OneEnumMember); //returns true Enum.IsDefined(typeof(MyEnum), "OtherEnumMember"); //returns true Enum.IsDefined(typeof(MyEnum), "SomethingDifferent"); //returns false 

If your question is like "I have an instance of type enum that has the Flags attribute, and I would like to have a function that tells if this instance contains a specific enumeration value, then the function looks like something like:

 public static bool ContainsValue<TEnum>(this TEnum e, TEnum val) where Enum: struct, IComparable, IFormattable, IConvertible { if (!e.GetType().IsEnum) throw new ArgumentException("The type TEnum must be an enum type.", nameof(TEnum)); dynamic val1 = e, val2 = val; return (val1 | val2) == val1; } 

Hope I can help.

+1
Mar 27 '17 at 18:32
source share



All Articles