C# Enum Format

Description

Enum Format converts the specified value of a specified enumerated type to its equivalent string representation according to the specified format.

Syntax

Enum.Format has the following syntax.


[ComVisibleAttribute(true)]//from   www  .  ja  v a  2  s.  c o m
public static string Format(
  Type enumType,
  Object value,
  string format
)

Parameters

Enum.Format has the following parameters.

  • enumType - The enumeration type of the value to convert.
  • value - The value to convert.
  • format - The output format to use.

Returns

Enum.Format method returns A string representation of value.

Example

The following example illustrates the use of Format in the context of Enum.


//  w  w  w  .j  a va 2  s  . c  o m
using System;

enum Colors { Red, Green, Blue, Yellow };

public class FormatTest {
    public static void Main() {
        Colors myColor = Colors.Blue;

        Console.WriteLine(Enum.Format(typeof(Colors), myColor, "d"));
        Console.WriteLine(Enum.Format(typeof(Colors), myColor, "x"));
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version