Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is C# enum ToString() guaranteed to return enum name?

Tags:

c#

enums

tostring

enum Flags
{
  Foo,
  Bar
}

Is Flags.Foo.ToString() guaranteed to return "Foo"? Or do I have to use Enum.GetName(...)?

like image 729
kaalus Avatar asked Jan 16 '15 13:01

kaalus


People also ask

Is C+ Same as C?

C++ was developed by Bjarne Stroustrup in 1979. C does no support polymorphism, encapsulation, and inheritance which means that C does not support object oriented programming. C++ supports polymorphism, encapsulation, and inheritance because it is an object oriented programming language. C is (mostly) a subset of C++.

What is 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 does %c mean in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

Is C -- a language?

C-- (pronounced C minus minus) is a C-like programming language. Its creators, functional programming researchers Simon Peyton Jones and Norman Ramsey, designed it to be generated mainly by compilers for very high-level languages rather than written by human programmers.


2 Answers

If the enum value happens to match an enum item, then yes.

But beware of cases like this:

var test = (Flags)(-1);
// test.ToString() == "-1"

If the value doesn't match an enum item, it will just return the underlying value as a string. By default, the underlying datatype of an enum is int.

Also, if your enum is defined with [Flags] like this:

[Flags]
enum Flags
{
    Foo = 1,
    Bar = 2
}

Then ToString() can return a comma-separated list of flags:

var test = Flags.Foo | Flags.Bar;
// test.ToString() == "Foo, Bar"

Like Orace points out in the comments, if the value is ambiguous, that is if multiple enum items can match the value, you should not make any assumptions about which one will be chosen.

like image 104
Lucas Trzesniewski Avatar answered Sep 22 '22 09:09

Lucas Trzesniewski


The return value is formatted with the general format specifier ("G"). That is, if the FlagsAttribute is not applied to this enumerated type and there is a named constant equal to the value of this instance, then the return value is a string containing the name of the constant. If the FlagsAttribute is applied and there is a combination of one or more named constants equal to the value of this instance, then the return value is a string containing a delimiter-separated list of the names of the constants. Otherwise, the return value is the string representation of the numeric value of this instance.

from MSDN

like image 27
David Pilkington Avatar answered Sep 21 '22 09:09

David Pilkington