ValueType.ToString 方法

定义

返回此实例的完全限定类型名称。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 方法将重写 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#由 @no__t 关键字定义的类型,以及 Visual Basic 中的 Structure ... @no__t 3 构造。)但在功能上,实现与 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 关键字定义的值类型和中的 Structure ... @no__t Visual Basic 通常会重写 @no__t 方法,以提供更有意义的值类型字符串表示形式。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. 它定义两个值类型,@no__t 0 和 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

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 为 @no__t 提供的支持的一部分(请参阅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. (此外,@no__t 中的结构不会继承 ValueType。)但是,当你在C#或 Visual Basic 代码中使用它们时,它们看起来有 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.

适用于