Home »
.Net »
C# Programs
C# - Enum.Format() Method with Example
In this tutorial, we will learn about the C# Enum.Format() method with its definition, usage, syntax, and example.
By Nidhi Last updated : March 29, 2023
C# Enum.Format() Method
The Enum.Format() method is used to convert the specified value of enum to string according to the specified format.
Syntax
string Enum.Format(Type enumType, object value, string format);
Parameter(s)
- enumType : Type that is enum.
- value : Object of enum.
- format : Format to represent enum value into the specified string format.
Return Value
This method returns string value according to the specified format.
Exception(s)
- System.ArgumentException
- System.ArgumentNullException
- System.InvalidOperationException
- System.FormatException
C# Example of Enum.Equals() Method
The source code to demonstrate the use of Format() method of Enum class is given below. The given program is compiled and executed successfully.
using System;
class Sample {
enum Color {
RED = 0, GREEN = 1, YELLOW = 3, WHITE = 4, BLACK = 5
};
//Entry point of Program
static public void Main() {
Color firstColor = Color.GREEN;
Color secondColor = Color.WHITE;
string strValue = "";
strValue = Enum.Format(typeof (Color), firstColor, "d");
Console.WriteLine(strValue);
strValue = Enum.Format(typeof (Color), secondColor, "x");
Console.WriteLine(strValue);
}
}
Output
1
00000004
Press any key to continue . . .
C# Enum Class Programs »