【摘MSDN】Enum.ToString 方法 (String)

本文转自:http://msdn.microsoft.com/zh-cn/vstudio/a0h36syw(VS.80).aspx

 

Enum.ToString 方法 (String)

使用指定格式将此实例的值转换成其等效的字符串表示。

命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)

Visual Basic(声明)
Public Function ToString ( _

            format As String _

            ) As String

            
Visual Basic(用法)
Dim instance As Enum

            Dim format As String

            Dim returnValue As String

            returnValue = instance.ToString(format)

            
C#
public string ToString (

            string format

            )

            
C++
public:

            String^ ToString (

            String^ format

            )

            
J#
public String ToString (

            String format

            )

            
JScript
public function ToString (

            format : String

            ) : String

            

参数

format

格式字符串。

返回值

此实例的值的字符串表示形式,由 format 指定。
异常类型 条件

FormatException

format 包含无效规范。

format 参数中可包含格式字符“G”或“g”、“D”或“d”、“X”或“x”、“F”或“f”。如果 format 是 空引用(在 Visual Basic 中为 Nothing) 或空字符串 (""),则使用一般格式说明符 ("G")。有关这些格式字符的更多信息,请参见 Format 方法的备注部分。有关一般格式化的更多信息,请参见 格式化概述

Visual Basic
' Sample for Enum.ToString(String)

            Imports System

            Class Sample

            Enum Colors

            Red

            Green

            Blue

            Yellow = 12

            End Enum 'Colors

            Public Shared Sub Main()

            Dim myColor As Colors = Colors.Yellow

            Console.WriteLine("Colors.Red = {0}", Colors.Red.ToString("d"))

            Console.WriteLine("Colors.Green = {0}", Colors.Green.ToString("d"))

            Console.WriteLine("Colors.Blue = {0}", Colors.Blue.ToString("d"))

            Console.WriteLine("Colors.Yellow = {0}", Colors.Yellow.ToString("d"))

            Console.WriteLine("{0}myColor = Colors.Yellow{0}", Environment.NewLine)

            Console.WriteLine("myColor.ToString(""g"") = {0}", myColor.ToString("g"))

            Console.WriteLine("myColor.ToString(""G"") = {0}", myColor.ToString("G"))

            Console.WriteLine("myColor.ToString(""x"") = {0}", myColor.ToString("x"))

            Console.WriteLine("myColor.ToString(""X"") = {0}", myColor.ToString("X"))

            Console.WriteLine("myColor.ToString(""d"") = {0}", myColor.ToString("d"))

            Console.WriteLine("myColor.ToString(""D"") = {0}", myColor.ToString("D"))

            Console.WriteLine("myColor.ToString(""f"") = {0}", myColor.ToString("f"))

            Console.WriteLine("myColor.ToString(""F"") = {0}", myColor.ToString("F"))

            End Sub 'Main

            End Class 'Sample

            '

            'This example produces the following results:

            '

            'Colors.Red = 0

            'Colors.Green = 1

            'Colors.Blue = 2

            'Colors.Yellow = 12

            '

            'myColor = Colors.Yellow

            '

            'myColor.ToString("g") = Yellow

            'myColor.ToString("G") = Yellow

            'myColor.ToString("x") = 0000000C

            'myColor.ToString("X") = 0000000C

            'myColor.ToString("d") = 12

            'myColor.ToString("D") = 12

            'myColor.ToString("f") = Yellow

            'myColor.ToString("F") = Yellow

            '

            
// Sample for Enum.ToString(String)

            using System;

            class Sample

            {

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

            public static void Main()

            {

            Colors myColor = Colors.Yellow;

            Console.WriteLine("Colors.Red = {0}", Colors.Red.ToString("d"));

            Console.WriteLine("Colors.Green = {0}", Colors.Green.ToString("d"));

            Console.WriteLine("Colors.Blue = {0}", Colors.Blue.ToString("d"));

            Console.WriteLine("Colors.Yellow = {0}", Colors.Yellow.ToString("d"));

            Console.WriteLine("{0}myColor = Colors.Yellow{0}", Environment.NewLine);

            Console.WriteLine("myColor.ToString(\"g\") = {0}", myColor.ToString("g"));

            Console.WriteLine("myColor.ToString(\"G\") = {0}", myColor.ToString("G"));

            Console.WriteLine("myColor.ToString(\"x\") = {0}", myColor.ToString("x"));

            Console.WriteLine("myColor.ToString(\"X\") = {0}", myColor.ToString("X"));

            Console.WriteLine("myColor.ToString(\"d\") = {0}", myColor.ToString("d"));

            Console.WriteLine("myColor.ToString(\"D\") = {0}", myColor.ToString("D"));

            Console.WriteLine("myColor.ToString(\"f\") = {0}", myColor.ToString("f"));

            Console.WriteLine("myColor.ToString(\"F\") = {0}", myColor.ToString("F"));

            }

            }

            /*

            This example produces the following results:

            Colors.Red = 0

            Colors.Green = 1

            Colors.Blue = 2

            Colors.Yellow = 12

            myColor = Colors.Yellow

            myColor.ToString("g") = Yellow

            myColor.ToString("G") = Yellow

            myColor.ToString("x") = 0000000C

            myColor.ToString("X") = 0000000C

            myColor.ToString("d") = 12

            myColor.ToString("D") = 12

            myColor.ToString("f") = Yellow

            myColor.ToString("F") = Yellow

            */

            
// Sample for Enum::ToString(String)

            using namespace System;

            enum class Colors

            {

            Red, Green, Blue, Yellow = 12

            };

            int main()

            {

            Colors myColor = Colors::Yellow;

            Console::WriteLine( "Colors::Red = {0}", Colors::Red.ToString( "d" ) );

            Console::WriteLine( "Colors::Green = {0}", Colors::Green.ToString( "d" ) );

            Console::WriteLine( "Colors::Blue = {0}", Colors::Blue.ToString( "d" ) );

            Console::WriteLine( "Colors::Yellow = {0}", Colors::Yellow.ToString( "d" ) );

            Console::WriteLine( " {0}myColor = Colors::Yellow {0}", Environment::NewLine );

            Console::WriteLine( "myColor->ToString(\"g\") = {0}", myColor.ToString( "g" ) );

            Console::WriteLine( "myColor->ToString(\"G\") = {0}", myColor.ToString( "G" ) );

            Console::WriteLine( "myColor->ToString(\"x\") = {0}", myColor.ToString( "x" ) );

            Console::WriteLine( "myColor->ToString(\"X\") = {0}", myColor.ToString( "X" ) );

            Console::WriteLine( "myColor->ToString(\"d\") = {0}", myColor.ToString( "d" ) );

            Console::WriteLine( "myColor->ToString(\"D\") = {0}", myColor.ToString( "D" ) );

            Console::WriteLine( "myColor->ToString(\"f\") = {0}", myColor.ToString( "f" ) );

            Console::WriteLine( "myColor->ToString(\"F\") = {0}", myColor.ToString( "F" ) );

            }

            /*

            This example produces the following results:

            Colors::Red = 0

            Colors::Green = 1

            Colors::Blue = 2

            Colors::Yellow = 12

            myColor = Colors::Yellow

            myColor->ToString("g") = Yellow

            myColor->ToString("G") = Yellow

            myColor->ToString("x") = 0000000C

            myColor->ToString("X") = 0000000C

            myColor->ToString("d") = 12

            myColor->ToString("D") = 12

            myColor->ToString("f") = Yellow

            myColor->ToString("F") = Yellow

            */

            
// Sample for Enum.ToString(String)

            import System.*;

            class Sample

            {

            enum Colors

            {

            red (0),

            green (1),

            blue (2),

            yellow (12);

            } //Colors

            public static void main(String[] args)

            {

            Colors myColor = Colors.yellow;

            Console.WriteLine("Colors.Red = {0}", Colors.red.ToString("d"));

            Console.WriteLine("Colors.Green = {0}", Colors.green.ToString("d"));

            Console.WriteLine("Colors.Blue = {0}", Colors.blue.ToString("d"));

            Console.WriteLine("Colors.Yellow = {0}", Colors.yellow.ToString("d"));

            Console.WriteLine("{0}myColor = Colors.Yellow{0}",

            Environment.get_NewLine());

            Console.WriteLine("myColor.ToString(\"g\") = {0}", myColor.ToString("g"));

            Console.WriteLine("myColor.ToString(\"G\") = {0}", myColor.ToString("G"));

            Console.WriteLine("myColor.ToString(\"x\") = {0}", myColor.ToString("x"));

            Console.WriteLine("myColor.ToString(\"X\") = {0}", myColor.ToString("X"));

            Console.WriteLine("myColor.ToString(\"d\") = {0}", myColor.ToString("d"));

            Console.WriteLine("myColor.ToString(\"D\") = {0}", myColor.ToString("D"));

            Console.WriteLine("myColor.ToString(\"f\") = {0}", myColor.ToString("f"));

            Console.WriteLine("myColor.ToString(\"F\") = {0}", myColor.ToString("F"));

            } //main

            } //Sample

            /*

            This example produces the following results:

            Colors.Red = 0

            Colors.Green = 1

            Colors.Blue = 2

            Colors.Yellow = 12

            myColor = Colors.Yellow

            myColor.ToString("g") = Yellow

            myColor.ToString("G") = Yellow

            myColor.ToString("x") = 0000000C

            myColor.ToString("X") = 0000000C

            myColor.ToString("d") = 12

            myColor.ToString("D") = 12

            myColor.ToString("f") = Yellow

            myColor.ToString("F") = Yellow

            */

            

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

你可能感兴趣的:(toString)