ValueType.ToString 方法


返回此实例的完全限定类型名称。Returns the fully qualified type name of this instance.

 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As 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 关键字定义的类型,以及 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,创建每个值类型,并调用其 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",};
      var empB = new EmployeeB{ Name = "Robert",};

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" }
      Dim empB = new EmployeeB With { .Name = "Robert" }
   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

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 应用商店应用和 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。)但是,当你在C#或 Visual Basic 代码中使用它们时,它们看起来有 ToStringEqualsGetHashCode 方法,而 .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.