The value parameter can be any of the following:
- Any member of type enumType.
- A variable whose value is an enumeration member of type enumType.
- The string representation of the name of an enumeration member. The characters in the string must have the same case as the enumeration member name.
- A value of the underlying type of enumType.
I believe that's the reason why it has no overload and takes object
as a second parameter. Since this method takes object
as a second parameter - and object
is a base class for all .NET types - you can pass string
or int
or etc..
Here how this method implemented;
public static bool IsDefined(Type enumType, Object value)
{
if (enumType == null)
throw new ArgumentNullException("enumType");
return enumType.IsEnumDefined(value);
}
And looks like this virtual Type.IsEnumDefined
method handles all of these cases in it's implementation like;
public virtual bool IsEnumDefined(object value)
{
if (value == null)
throw new ArgumentNullException("value");
if (!IsEnum)
throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
Contract.EndContractBlock();
// Check if both of them are of the same type
Type valueType = value.GetType();
// If the value is an Enum then we need to extract the underlying value from it
if (valueType.IsEnum)
{
if (!valueType.IsEquivalentTo(this))
throw new ArgumentException(Environment.GetResourceString("Arg_EnumAndObjectMustBeSameType", valueType.ToString(), this.ToString()));
valueType = valueType.GetEnumUnderlyingType();
}
// If a string is passed in
if (valueType == typeof(string))
{
string[] names = GetEnumNames();
if (Array.IndexOf(names, value) >= 0)
return true;
else
return false;
}
// If an enum or integer value is passed in
if (Type.IsIntegerType(valueType))
{
Type underlyingType = GetEnumUnderlyingType();
// We cannot compare the types directly because valueType is always a runtime type but underlyingType might not be.
if (underlyingType.GetTypeCodeImpl() != valueType.GetTypeCodeImpl())
throw new ArgumentException(Environment.GetResourceString("Arg_EnumUnderlyingTypeAndObjectMustBeSameType", valueType.ToString(), underlyingType.ToString()));
Array values = GetEnumRawConstantValues();
return (BinarySearch(values, value) >= 0);
}
}