C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Typeof: The typeof operator can be passed as the second argument to Enum.Format.
Typeof, nameofC# program that demonstrates Enum.Format
using System;
class Program
{
enum Importance
{
Low,
Medium,
Critical
}
static void Main()
{
M("G");
M("g");
M("X");
M("x");
M("F");
M("f");
M("D");
M("d");
}
static void M(string format)
{
// Use Enum.Format with the specified format string.
string value = Enum.Format(typeof(Importance), Importance.Critical, format);
Console.WriteLine("{0} = {1}", format, value);
}
}
Output
G = Critical
g = Critical
X = 00000002
x = 00000002
F = Critical
f = Critical
D = 2
d = 2
Tip: You could do this by casting to the underlying type and then using ToString, but Format is another alternative.
ToStringTypically: The Enum.Format method is not needed but could provide a useful bit of functionality for some programs.