Movatterモバイル変換


[0]ホーム

URL:


Skip to main contentSkip to in-page navigation

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

ValueType.ToString Method

Definition

Namespace:
System
Assemblies:
mscorlib.dll, System.Runtime.dll
Assemblies:
netstandard.dll, System.Runtime.dll
Assembly:
System.Runtime.dll
Assembly:
mscorlib.dll
Assembly:
netstandard.dll
Source:
ValueType.cs
Source:
ValueType.cs
Source:
ValueType.cs
Source:
ValueType.cs

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Returns the fully qualified type name of this instance.

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

Returns

The fully qualified type name.

Remarks

TheValueType.ToString method overrides theObject.ToString method and provides the default implementation of theToString method for value types. (Value types are types defined by thestruct keyword in C#, and by theStructure...End Structure construct in Visual Basic.) Functionally, however, the implementation is that same as that ofObject.ToString: the method returns the fully qualified type name.

Value types defined by thestruct keyword in C# and theStructure...End Structure construct in Visual Basic typically override theValueType.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 andEmployeeB, creates an instance of each, and calls itsToString method. Because theEmployeeA structure does not override theValueType.ToString method, it displays only the fully qualified type name. TheEmployeeB.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
namespace Corporate.EmployeeObjects[<Struct>]type EmployeeA =    val mutable Name : string[<Struct>]type EmployeeB =    val mutable Name : string    override this.ToString() =           this.Namemodule Example =     let empA = EmployeeA(Name="Robert")     printfn $"{empA}"     let empB = EmployeeB(Name="Robert")     printfn $"{empB}"// The example displays the following output://     Corporate.EmployeeObjects.EmployeeA//     Robert
Imports Corporate.EmployeeObjectsModule 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 SubEnd ModuleNamespace 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

Note that, although enumeration types are also value types, they derive from theEnum class, which overridesValueType.ToString.

Notes for the Windows Runtime

When you call theToString method on a Windows Runtime structure, it provides the default behavior for value types that don't overrideToString. 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 overrideToString, 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 inheritValueType.) However, they appear to haveToString,Equals, andGetHashCode methods when you use them in your C# or Visual Basic code, and .NET provides the default behavior for these methods.

Applies to

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?