Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Enum Format Method
The Enum.Format method in C# converts the value of a specified enumerated type to its equivalent string representation according to the specified format. This method provides flexible formatting options including decimal, hexadecimal, and name representations.
Syntax
Following is the syntax for the Enum.Format method −
public static string Format(Type enumType, object value, string format)
Parameters
enumType − The enumeration type of the value to convert.
value − The value to convert.
format − The output format to use. Common formats are "G" (name), "D" (decimal), "X" (hexadecimal), and "F" (name if defined, otherwise decimal).
Format Specifiers
| Format | Description | Example Output |
|---|---|---|
| "G" or "g" | Returns the string name of the enumeration | Keyboard |
| "D" or "d" | Returns the decimal equivalent | 1 |
| "X" or "x" | Returns the hexadecimal equivalent | 1 |
| "F" or "f" | Returns the name if defined, otherwise the decimal value | Keyboard |
Using Enum.Format with Different Format Specifiers
Example
using System;
class Demo {
enum Stock { PenDrive, Keyboard, Speakers };
static void Main() {
Stock st = Stock.Keyboard;
Console.WriteLine("Enum value: {0}", st);
Console.WriteLine("Name format (G): {0}", Enum.Format(typeof(Stock), st, "G"));
Console.WriteLine("Decimal format (D): {0}", Enum.Format(typeof(Stock), st, "D"));
Console.WriteLine("Hexadecimal format (X): {0}", Enum.Format(typeof(Stock), st, "X"));
Console.WriteLine("Flag format (F): {0}", Enum.Format(typeof(Stock), st, "F"));
}
}
The output of the above code is −
Enum value: Keyboard Name format (G): Keyboard Decimal format (D): 1 Hexadecimal format (X): 1 Flag format (F): Keyboard
Using Enum.Format with Custom Values
Example
using System;
class Program {
enum Priority { Low = 10, Medium = 20, High = 30, Critical = 100 };
static void Main() {
Priority p = Priority.Critical;
Console.WriteLine("Priority name: {0}", Enum.Format(typeof(Priority), p, "G"));
Console.WriteLine("Priority decimal value: {0}", Enum.Format(typeof(Priority), p, "D"));
Console.WriteLine("Priority hex value: {0}", Enum.Format(typeof(Priority), p, "X"));
// Format using integer value
Console.WriteLine("Format value 20: {0}", Enum.Format(typeof(Priority), 20, "G"));
}
}
The output of the above code is −
Priority name: Critical Priority decimal value: 100 Priority hex value: 64 Format value 20: Medium
Using Enum.Format with Flag Enums
Example
using System;
class FlagDemo {
[Flags]
enum FileAccess { Read = 1, Write = 2, Execute = 4 };
static void Main() {
FileAccess access = FileAccess.Read | FileAccess.Write;
Console.WriteLine("Combined flags (G): {0}", Enum.Format(typeof(FileAccess), access, "G"));
Console.WriteLine("Combined flags (D): {0}", Enum.Format(typeof(FileAccess), access, "D"));
Console.WriteLine("Combined flags (F): {0}", Enum.Format(typeof(FileAccess), access, "F"));
}
}
The output of the above code is −
Combined flags (G): Read, Write Combined flags (D): 3 Combined flags (F): Read, Write
Conclusion
The Enum.Format method provides a flexible way to convert enum values to their string representations using various format specifiers. It supports name formatting (G), decimal formatting (D), hexadecimal formatting (X), and flag formatting (F) for different use cases.
