

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Enum Format Method
The Format method converts value of a specified enumerated type to its equivalent string representation. Here you can also set the format i.e. d for Decimal, x for HexaDecimal, etc.
We have the following enumeration.
enum Stock { PenDrive, Keyboard, Speakers };
The default value gets assigned (initialize).
PenDrive = 0 Keyboard = 1 Speakers = 2
Now, let’s say you want the value of “Keyboard” name.
Stock st = Stock.Keyboard;
For that, try the following and get the constant value for Keyboard name.
Enum.Format(typeof(Stock), st, "d")
The following is the entire example.
Example
using System; class Demo { enum Stock { PenDrive, Keyboard, Speakers }; static void Main() { Stock st = Stock.Keyboard; Console.WriteLine("Product I need is {0}", st); Console.WriteLine("Product value: {0}", Enum.Format(typeof(Stock), st, "d")); } }
Output
Product I need is Keyboard Product value: 1
- Related Questions & Answers
- C# Enum IsDefined Method
- C# Enum ToString() Method
- C# Enum Parse Method
- C# Enum TryParse() Method
- C# Enum CompareTo Method
- C# Enum Equals Method
- C# Enum GetName Method
- C# Enum GetNames Method
- C# Enum GetValues Method
- Java String format() method example.
- LocalDateTime format() method in Java
- LocalTime format() method in Java
- MonthDay format() method in Java
- LocalDate format() method in Java
- Can we define an enum inside a method in Java?
Advertisements