19

C# .NET 4.5, Windows 10, I have the following enum:

private enum Enums
{
    A=1, B=2, C=3
}

And this program behaves in a very strange way:

public static void Main()
{
    Enums e;
    if (Enum.TryParse("12", out e))
    {
        Console.WriteLine("Parsed {0}", e);
    }
    else
    {
        Console.Write("Not parsed");
    }
    Console.ReadLine();
}

I would expect the result of the TryParse method to be false, but to my surprise the console shows "Parsed 12". In the Watch window it even shows that the value is "12" and it is of the Enums type!

This is true for any number string that I tried (e.g. "540"), but not for strings that include letters ("A12", "12A").

I can easily overcome this by first checking if it's a number-only string, but why is this the behaviour? Is it by design?

Thanks! Ido

Ido Cohn
  • 1,685
  • 3
  • 21
  • 28
  • 1
    Yes, it is by design. It is a good idea that if `.ToString()` on a value can return a specific string, and the type in question has a `.Parse()` method, then the parse method should accept that string in return. Since enums can hold "unknown" values (ie. values that doesn't have a name in the enum), and `.ToString()` will return these as the number in a string, then it follows that `.Parse()` should accept a string holding only an integer number. So yes, this is by design. You may disagree with this decision but it is what it is. – Lasse V. Karlsen Jan 31 '16 at 18:14

3 Answers3

10

Internally, enums are stored as integers so that's likely why TryParse is returning true for integers being passed in.

Regarding why any integer is working, it's by design. From MSDN (emphasis mine):

When this method returns, result contains an object of type TEnum whose value is represented by value if the parse operation succeeds. If the parse operation fails, result contains the default value of the underlying type of TEnum. Note that this value need not be a member of the TEnum enumeration. This parameter is passed uninitialized.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • 4
    Thanks! I wasn't looking in the right place.. Also, apparently from the MSDN: If this behavior is undesirable, call the IsDefined method to ensure that a particular string representation of an integer is actually a member of TEnum. – Ido Cohn Jan 31 '16 at 18:05
  • You're welcome. Yes, using `IsDefined` would help if there's a chance that a non-member integer could be passed in. You could create an extension method that handles the `IsDefined` check for you after the parse so that you can use that method instead of the default `TryParse`. – keyboardP Jan 31 '16 at 18:06
6

A variable or field of an enumeration type can hold any values of its underlying type, so storing the value of 12 in a variable of type Enums in your case is entirely legal:

var e = (Enums) 12;
var i = (int) e; // i is 12

Therefore, Enum.TryParse must be able to parse any value of type int (or whichever underlying integer type used in your enumeration).

If you want to reject values having no representation in your enumeration, check them with Enum.IsDefined.

ach
  • 2,314
  • 1
  • 13
  • 23
6

This method strictly parses integers to the range of the enum:

public static bool EnumTryParseStrict<TEnum>(string value, out TEnum result, bool ignoreCase = false) where TEnum : struct, Enum {
        if (Enum.TryParse(value, ignoreCase, out result) && Enum.IsDefined(typeof(TEnum), result)) {
            return true;
        }
        result = default;
        return false;
}

The method can be placed in a static class like this:

namespace IdentityStream { 
    public static class EnumUtil { 
        public static bool TryParseStrict<TEnum> ...
    }
}

result = default; assigns the default value of the underlying type of TEnum when the parse operation fails. This was done to comply with an expectation set by the Enum.TryParse contract:

If the parse operation fails, result contains the default value of the underlying type of TEnum