ValueType.ToString 方法

定義

傳回此執行個體的完整類型名稱。Returns the fully qualified type name of this instance.

public:
 override System::String ^ ToString();
public override string ToString ();
public override string? ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

傳回

String

完整類型名稱。The fully qualified type name.

備註

ValueType.ToString方法會覆寫 Object.ToString 方法,並為實值型別提供方法的預設實 ToString 值。The ValueType.ToString method overrides the Object.ToString method and provides the default implementation of the ToString method for value types. (實值型別是 c # 中的關鍵字所定義的型別 struct ,以及 Structure End Structure Visual Basic 中的 ... 結構。 ) 功能上,其實作與的相同 Object.ToString :方法會傳回完整的型別名稱。(Value types are types defined by the struct keyword in C#, and by the Structure...End Structure construct in Visual Basic.) Functionally, however, the implementation is that same as that of Object.ToString: the method returns the fully qualified type name.

C # 中的關鍵字所定義的實數值型別 structStructure End Structure Visual Basic 中的 ... 結構通常會覆寫 ValueType.ToString 方法,以提供更有意義的實值型別字符串表示。Value types defined by the struct keyword in C# and the Structure...End Structure construct in Visual Basic typically override the ValueType.ToString method to provide a more meaningful string representation of the value type. 下列範例會說明其間的差異。The following example illustrates the difference. 它會定義兩個實值型別, EmployeeA 以及 EmployeeB 建立每一個的實例,並呼叫其 ToString 方法。It defines two value types, EmployeeA and EmployeeB, creates an instance of each, and calls its ToString method. 由於 EmployeeA 結構不會覆寫 ValueType.ToString 方法,因此只會顯示完整的類型名稱。Because the EmployeeA structure does not override the ValueType.ToString method, it displays only the fully qualified type name. 另一方面 EmployeeB.ToString ,方法會提供有關物件的有意義資訊。The EmployeeB.ToString method, on the other hand, provides meaningful information about the object.

using System;
using Corporate.EmployeeObjects;

public class Example
{
   public static void Main()
   {
      var empA = new EmployeeA{ Name = "Robert",};
      Console.WriteLine(empA.ToString());
      
      var empB = new EmployeeB{ Name = "Robert",};
      Console.WriteLine(empB.ToString());
   }
}

namespace Corporate.EmployeeObjects
{
    public struct EmployeeA
    {
         public String Name { get; set; }
    }
    
    public struct EmployeeB
    {
         public String Name { get; set; }

         public override String ToString()
         {
              return Name;
         }
    }  
}
// The example displays the following output:
//     Corporate.EmployeeObjects.EmployeeA
//     Robert
Imports Corporate.EmployeeObjects

Module Example
   Public Sub Main()
      Dim empA As New EmployeeA With { .Name = "Robert" }
      Console.WriteLine(empA.ToString())
      
      Dim empB = new EmployeeB With { .Name = "Robert" }
      Console.WriteLine(empB.ToString())
   End Sub
End Module

Namespace Corporate.EmployeeObjects
    Public Structure EmployeeA
         Public Property Name As String 
    End Structure
    
    Public Structure EmployeeB
         Public Property Name As String 

         Public Overrides Function ToString() As String 
              Return Name
         End Function
    End Structure  
End Namespace
' The example displays the following output:
'     Corporate.EmployeeObjects.EmployeeA
'     Robert

請注意,雖然列舉型別也是實值型別,但是它們衍生自會 Enum 覆寫的類別 ValueType.ToStringNote that, although enumeration types are also value types, they derive from the Enum class, which overrides ValueType.ToString.

Windows 執行階段的注意事項Notes for the Windows Runtime

當您 ToString 在 Windows 執行階段結構上呼叫方法時,它會為不會覆寫的實數值型別提供預設行為 ToStringWhen you call the ToString method on a Windows Runtime structure, it provides the default behavior for value types that don't override ToString. 這是 .NET Framework 為 Windows 執行階段提供的支援的一部分 (請參閱 .NET Framework 支援 Windows Store 應用程式和 Windows 執行階段) 。This is part of the support that the .NET Framework provides for the Windows Runtime (see .NET Framework Support for Windows Store Apps and Windows Runtime). Windows 執行階段結構無法覆寫 ToString ,即使它們是使用 c # 或 Visual Basic 撰寫的,因為它們不能有方法。Windows Runtime structures can't override ToString, even if they're written with C# or Visual Basic, because they can't have methods. (此外,Windows 執行階段本身的結構並不會繼承 ValueType 。 ) 不過, ToString Equals GetHashCode 當您在 c # 或 Visual Basic 程式碼中使用它們時,它們看起來會有、和方法,而 .NET Framework 則提供這些方法的預設行為。(In addition, structures in the Windows Runtime itself don't inherit ValueType.) However, they appear to have ToString, Equals, and GetHashCode methods when you use them in your C# or Visual Basic code, and the .NET Framework provides the default behavior for these methods.

適用於