117

I have a Enum like this:

 public enum PromotionTypes
{
    Unspecified = 0, 
    InternalEvent = 1,
    ExternalEvent = 2,
    GeneralMailing = 3,  
    VisitBased = 4,
    PlayerIntroduction = 5,
    Hospitality = 6
}

I want to check if this Enum contain a number I give. For example: When I give 4, Enum contain that, So I want to return True, If I give 7, There isn't 7 in this Enum, So it returns False. I tried Enum.IsDefine but it only check the String value. How can I do that?

John Woo
  • 258,903
  • 69
  • 498
  • 492
Jack Zhang
  • 2,534
  • 5
  • 25
  • 34
  • Possible duplicate of [Validate Enum Values](http://stackoverflow.com/questions/13615/validate-enum-values) – bluish Feb 22 '16 at 10:02

5 Answers5

249

The IsDefined method requires two parameters. The first parameter is the type of the enumeration to be checked. This type is usually obtained using a typeof expression. The second parameter is defined as a basic object. It is used to specify either the integer value or a string containing the name of the constant to find. The return value is a Boolean that is true if the value exists and false if it does not.

enum Status
{
    OK = 0,
    Warning = 64,
    Error = 256
}

static void Main(string[] args)
{
    bool exists;

    // Testing for Integer Values
    exists = Enum.IsDefined(typeof(Status), 0);     // exists = true
    exists = Enum.IsDefined(typeof(Status), 1);     // exists = false

    // Testing for Constant Names
    exists = Enum.IsDefined(typeof(Status), "OK");      // exists = true
    exists = Enum.IsDefined(typeof(Status), "NotOK");   // exists = false
}

SOURCE

John Woo
  • 258,903
  • 69
  • 498
  • 492
12

Try this:

IEnumerable<int> values = Enum.GetValues(typeof(PromotionTypes))
                              .OfType<PromotionTypes>()
                              .Select(s => (int)s);
if(values.Contains(yournumber))
{
      //...
}
horgh
  • 17,918
  • 22
  • 68
  • 123
11

You should use Enum.IsDefined.

I tried Enum.IsDefine but it only check the String value.

I'm 100% sure it will check both string value and int(the underlying) value, at least on my machine.

Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • 1
    Thx, It's my mistakes, I forgot to convert the string to Int, so Enum.isDefined always get false when i gave the right number. – Jack Zhang Sep 06 '12 at 05:45
  • It definitely can take the (case-sensitive string representation) - see [the docs](https://learn.microsoft.com/en-us/dotnet/api/system.enum.isdefined?view=netframework-4.7.1#remarks) or [the source](https://referencesource.microsoft.com/#mscorlib/system/enum.cs,32e813377ac50d28) for more info. – Wai Ha Lee May 10 '18 at 09:18
  • `if (Enum.IsDefined((CustomType)customTypeId)) { ... }` works great! – Benxamin Aug 05 '23 at 00:01
5

Maybe you want to check and use the enum of the string value:

string strType;
if(Enum.TryParse(strType, out MyEnum myEnum))
{
    // use myEnum
}
Prem
  • 303
  • 2
  • 9
Hulzi
  • 59
  • 1
  • 2
0

I use Enums.NET package.

public enum PromotionTypes
{
    Unspecified = 0, 
    InternalEvent = 1,
    ExternalEvent = 2,
    GeneralMailing = 3,  
    VisitBased = 4,
    PlayerIntroduction = 5,
    Hospitality = 6
}

((PromotionTypes)4).IsValid() == true;
((PromotionTypes)7).IsValid() == false;
George Paoli
  • 2,257
  • 1
  • 24
  • 29