Прочетете на английски Редактиране

Споделяне чрез


ValueType.ToString Method

Definition

Returns the fully qualified type name of this instance.

C#
public override string ToString();
C#
public override string? ToString();

Returns

The fully qualified type name.

Remarks

The ValueType.ToString method overrides the Object.ToString method and provides the default implementation of the ToString method for value types. (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.

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. It defines two value types, EmployeeA and EmployeeB, creates an instance of each, and calls its ToString method. Because the EmployeeA structure does not override the ValueType.ToString method, it displays only the fully qualified type name. The EmployeeB.ToString method, on the other hand, provides meaningful information about the object.

C#
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

Note that, although enumeration types are also value types, they derive from the Enum class, which overrides ValueType.ToString.

Notes for the Windows Runtime

When you call the ToString method on a Windows Runtime structure, it provides the default behavior for value types that don't override ToString. This is part of the support that .NET provides for the Windows Runtime (see .NET Support for Windows Store Apps and Windows Runtime). Windows Runtime structures can't override ToString, even if they're written with C# or Visual Basic, because they can't have methods. (In addition, structures in the 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 .NET provides the default behavior for these methods.

Applies to

Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0