Ido Cohn
Ido Cohn

Reputation: 1705

C# Enum.TryParse parses invalid number strings

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

Upvotes: 20

Views: 7018

Answers (3)

Tore Olav Kristiansen
Tore Olav Kristiansen

Reputation: 325

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

Upvotes: 9

ach
ach

Reputation: 2373

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.

Upvotes: 6

keyboardP
keyboardP

Reputation: 69382

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.

Upvotes: 10

Related Questions