enum Flags
{
Foo,
Bar
}
Is Flags.Foo.ToString()
guaranteed to return "Foo"? Or do I have to use Enum.GetName(...)?
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++.
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 ...
%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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With