Converting Enumerated type to String according to the Specified Format in C#

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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"));
    }
}

chevron_right


Output:

My favorite Animal is Cat.
The value of my favorite Animal is 1.
The hex value of my Animal  is 00000001.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


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:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.