ValueType.ToString 方法

定義

傳回此執行個體的完整類型名稱。

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

傳回

完整類型名稱。

備註

方法 ValueType.ToString 會覆寫 方法, Object.ToString 併為實值型別提供方法的默認實 ToString 作。 (Value 類型是由 C# 中的 關鍵詞所定義,以及 StructureVisual Basic.) Functionally 中的 ...End Structure 建構所定義的struct類型,不過,實作與 的相同Object.ToString:方法會傳回完整型別名稱。

C# 中的 關鍵詞所 struct 定義的實值型別和 StructureVisual Basic 中的 ...End Structure 建構通常會覆寫 ValueType.ToString 方法,以提供更有意義的實值型別表示。 下列範例會說明其間的差異。 它會定義兩個實值型別, EmployeeAEmployeeB會建立每個類型的實例,並呼叫其 ToString 方法。 EmployeeA因為結構不會覆寫 ValueType.ToString 方法,所以只會顯示完整型別名稱。 EmployeeB.ToString另一方面,方法會提供有關 物件的有意義資訊。

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
namespace Corporate.EmployeeObjects

[<Struct>]
type EmployeeA =
    val mutable Name : string

[<Struct>]
type EmployeeB =
    val mutable Name : string
    override this.ToString() = 
          this.Name

module Example =
     let empA = EmployeeA(Name="Robert")
     printfn $"{empA}"

     let empB = EmployeeB(Name="Robert")
     printfn $"{empB}"
// 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.ToString

Windows 執行階段 注意事項

當您在 Windows 執行階段 結構上呼叫 ToString 方法時,它會提供未覆寫 ToString之實值型別的默認行為。 這是 .NET 為 Windows 執行階段 (提供的支援的一部分,請參閱 Windows 市集應用程式的 .NET 支援和 Windows 執行階段) 。 Windows 執行階段 結構無法覆寫 ToString,即使它們是以 C# 或 Visual Basic 撰寫,因為它們不能有方法。 (此外,Windows 執行階段 本身中的結構不會繼承 ValueType.) 不過,當您在 C# 或 Visual Basic 程式代碼中使用結構時,它們看起來會有 ToStringEqualsGetHashCode 方法,而 .NET 會提供這些方法的預設行為。

適用於