Export (0) Print
Expand All

Enum.Format Method

Converts the specified value of a specified enumerated type to its equivalent string representation according to the specified format.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)] 
public static string Format (
	Type enumType,
	Object value,
	string format
)
/** @attribute ComVisibleAttribute(true) */ 
public static String Format (
	Type enumType, 
	Object value, 
	String format
)
ComVisibleAttribute(true) 
public static function Format (
	enumType : Type, 
	value : Object, 
	format : String
) : String
Not applicable.

Parameters

enumType

The enumeration type of the value to convert.

value

The value to convert.

format

The output format to use.

Return Value

A string representation of value.

Exception typeCondition

ArgumentNullException

The enumType, value, or format parameter is a null reference (Nothing in Visual Basic).

ArgumentException

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

The format parameter contains an invalid value.

The valid format values are:

Format

Description

"G" or "g"

If value is equal to a named enumerated constant, the name of that constant is returned; otherwise, the decimal equivalent of value is returned.

For example, suppose the only enumerated constant is named, Red, and its value is 1. If value is specified as 1, then this format returns "Red". However, if value is specified as 2, this format returns "2".

-or-

If the FlagsAttribute custom attribute is applied to the enumeration, then value is treated as a bit field that contains one or more flags that consist of one or more bits.

If value is equal to a combination of named enumerated constants, a delimiter-separated list of the names of those constants is returned. value is searched for flags, going from the flag with the largest value to the smallest value. For each flag that corresponds to a bit field in value, the name of the constant is concatenated to the delimiter-separated list. Then the value of that flag is excluded from further consideration, and the search continues for the next flag.

If value is not equal to a combination of named enumerated constants, the decimal equivalent of value is returned.

"X" or "x"

Represents value in hexadecimal without a leading "0x".

"D" or "d"

Represents value in decimal form.

"F" or "f"

Behaves identically to "G" or "g", except the FlagsAttribute is not required to be present on the Enum declaration.

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

using System;

public class FormatTest {
    enum Colors { Red, Green, Blue, Yellow };

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

        Console.WriteLine("My favorite color is {0}", myColor);
        Console.WriteLine("The value of my favorite color is {0}", Enum.Format(typeof(Colors), myColor, "d"));
        Console.WriteLine("The hex value of my favorite color is {0}", Enum.Format(typeof(Colors), myColor, "x"));
    }
}

import System.*;

public class FormatTest
{
    enum Colors
    {
        red (0),
        green (1),
        blue (2),
        yellow (3);
    } //Colors

    public static void main(String[] args)
    {
        Colors myColor = Colors.blue;

        Console.WriteLine("My favorite color is {0}", myColor);
        Console.WriteLine("The value of my favorite color is {0}", 
            Enum.Format(Colors.class.ToType(), myColor, "d"));
        Console.WriteLine("The hex value of my favorite color is {0}", 
            Enum.Format(Colors.class.ToType(), myColor, "x"));
    } //main
} //FormatTest

import System;

public class FormatTest {
    enum Colors { Red, Green, Blue, Yellow };

    public static function Main() {
        var myColor : Colors = Colors.Blue;

        Console.WriteLine("My favorite color is {0}", myColor);
        Console.WriteLine("The value of my favorite color is {0}", Enum.Format(Colors, myColor, "d"));
        Console.WriteLine("The hex value of my favorite color is {0}", Enum.Format(Colors, myColor, "x"));
    }
}

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft