Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Enum.TryParse parses invalid number strings

Tags:

c#

enums

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

like image 888
Ido Cohn Avatar asked Jan 31 '16 17:01

Ido Cohn


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


2 Answers

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.

like image 103
keyboardP Avatar answered Sep 29 '22 23:09

keyboardP


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.

like image 28
ach Avatar answered Sep 30 '22 00:09

ach