ValueType.ToString ValueType.ToString ValueType.ToString ValueType.ToString Method

定義

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

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

傳回

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

備註

@No__t-0 方法會覆寫 @no__t 1 方法,並為實數值型別提供 @no__t 2 方法的預設執行。The ValueType.ToString method overrides the Object.ToString method and provides the default implementation of the ToString method for value types. (實數值型別是中C#的 struct 關鍵字所定義的類型,以及 Visual Basic 中的 Structure ... End Structure 結構。)不過,功能上的實現與 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#的 struct 關鍵字所定義的實數值型別和 Visual Basic 中的 Structure ... End Structure 結構通常會覆寫 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. 它會定義兩個實數值型別,EmployeeAEmployeeB,建立每個的實例,並呼叫其 @no__t 2 方法。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

請注意,雖然列舉型別也是實值型別,但它們衍生自 @no__t 0 類別,它會覆寫 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

當您在 Windows 執行階段Windows Runtime 結構上呼叫 ToString 方法時,它會為不會覆寫 ToString 的實數值型別提供預設行為。When you call the ToString method on a Windows 執行階段Windows Runtime structure, it provides the default behavior for value types that don't override ToString. 這是 .NET Framework 提供給 Windows 執行階段Windows Runtime 的支援之一部分(請參閱Windows Store 應用程式和 Windows 執行階段的 .NET Framework 支援)。This is part of the support that the .NET Framework provides for the Windows 執行階段Windows Runtime (see .NET Framework Support for Windows Store Apps and Windows Runtime). Windows 執行階段Windows Runtime 結構不能覆寫 ToString,即使它們是以C#或 Visual Basic 撰寫,因為它們不能有方法。structures can't override ToString, even if they're written with C# or Visual Basic, because they can't have methods. (此外,Windows 執行階段Windows Runtime 本身的結構不會繼承 ValueType)。不過,當您在或 Visual Basic 程式C#代碼中使用它們時,它們似乎會有 ToStringEquals 和 @no__t 2 方法,而 .NET Framework 會提供這些方法的預設行為。(In addition, structures in the Windows 執行階段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.

適用於