Skip to content
Related Articles

Related Articles

Converting Enumerated type to String according to the Specified Format in C#
  • Last Updated : 23 Jul, 2019

Enum.Format(Type, Object, String) Method is used to convert the specified value of a specified enumerated type to its equivalent string representation according to the specified format.

Syntax:

public static string Format (Type enumType, object value, string format);

Parameters:

  • enumType: It is the enumeration type of the value to convert.
  • value: It is the value to convert.
  • format: It is the output format to use.

Returns: This method returns the string representation of value.

Exceptions:



  • ArgumentNullException: If the enumType, value, or format parameter is null.
  • ArgumentException: If the enumType parameter is not an Enum type or the value is from an enumeration that differs in type from enumType or the type of value is not an underlying type of enumType.
  • FormatException: If the format parameter contains an invalid value.
  • InvalidOperationException: If the format equals “X”, but the enumeration type is unknown.

Below programs illustrate the use of the above-discussed method.

Example 1:




// C# program to illustrate the
// Enum.Format(Type, Object,
// String) Method
using System;
  
enum Animals { Dog,
               Cat,
               Cow };
  
class GFG {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        Animals fav = Animals.Cat;
  
        Console.WriteLine("My favorite Animal is {0}.", fav);
  
        // using the Method and here
        // "d" specify the value in
        // decimal form.
        Console.WriteLine("The value of my favorite Animal is {0}.",
                          Enum.Format(typeof(Animals), fav, "d"));
  
        // using the Method and here
        // "x" specify the value in
        // hexadecimal form.
        Console.WriteLine("The hex value of my Animal  is {0}.",
                          Enum.Format(typeof(Animals), fav, "x"));
    }
}
Output:
My favorite Animal is Cat.
The value of my favorite Animal is 1.
The hex value of my Animal  is 00000001.

Example 2:




// C# program to illustrate the
// Enum.Format(Type, Object,
// String) Method
using System;
  
enum Animals { Dog,
               Cat,
               Cow };
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        Animals fav = Animals.Cat;
  
        Console.WriteLine("My favorite Animal is {0}.", fav);
  
        // using the Method and format is null
        // thats why it give exception
        Console.WriteLine("The value of my favorite Animal is {0}.",
                          Enum.Format(typeof(Animals), fav, null));
    }
}

Runtime Error:

Unhandled Exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: format

Note: Below are some valid values for the format parameter.

  • “D or “d” : It represents the value parameter in decimal form.
  • “X” or “x” : It represents the value parameter in hexadecimal format. It doesn’t lead “0x”.

Reference:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :