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.

注解

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. (值类型是定义的类型struct关键字在 C# 中,并通过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.

值定义的类型structC# 中的关键字和Structure...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. 它定义了两个值类型,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",};
      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 运行时Windows Runtime结构,它不会覆盖的值类型提供的默认行为ToStringWhen 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(请参阅.NET Framework 支持的 Windows 应用商店应用程序和 Windows 运行时)。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。)但是,它们显示为具有ToStringEquals,和GetHashCode方法在 C# 或 Visual Basic 代码中,使用它们以及.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.

适用于