ValueType.ToString 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
이 인스턴스의 정규화된 형식 이름을 반환합니다.
public:
override System::String ^ ToString();
public override string ToString();
public override string? ToString();
override this.ToString : unit -> string
Public Overrides Function ToString () As String
반품
정규화된 형식 이름입니다.
설명
메서드는 ValueType.ToString 메서드를 Object.ToString 재정의하고 값 형식에 대한 메서드의 기본 구현을 ToString 제공합니다. (값 형식은 C#의 struct 키워드 및 Structure에 의해 정의된 형식입니다. End Structure Visual Basic 구문) 그러나 기능적으로 구현은 Object.ToString와 동일합니다. 메서드는 정규화된 형식 이름을 반환합니다.
C#의 struct 키워드 및 Structure... Visual Basic End Structure 구문은 일반적으로 ValueType.ToString 메서드를 재정의하여 값 형식의 보다 의미 있는 문자열 표현을 제공합니다. 다음 예제에서 차이점을 보여 줍니다. 두 가지 값 형식 EmployeeA 을 정의하고 EmployeeB각 인스턴스를 만들고 해당 메서드를 호출합니다 ToString . 구조체는 EmployeeA 메서드를 재정의 ValueType.ToString 하지 않으므로 정규화된 형식 이름만 표시합니다. 반면에 이 메서드는 EmployeeB.ToString 개체에 대한 의미 있는 정보를 제공합니다.
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.Name
module 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.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.ToString.
Windows 런타임에 대한 참고 사항
Windows 런타임 구조체에서 ToString 메서드를 호출하는 경우 ToString 재정의하지 않는 값 형식에 대한 기본 동작을 제공합니다. 이는 .NET이 Windows 런타임에 대해 제공하는 지원의 일부입니다( Windows 스토어 앱 및 Windows 런타임에 대한 .NET 지원 참조). Windows 런타임 구조체는 C# 또는 Visual Basic 작성되더라도 메서드를 사용할 수 없으므로 ToString 재정의할 수 없습니다. 또한 Windows 런타임 자체의 구조체는 ValueType 상속하지 않습니다. 그러나 C# 또는 Visual Basic 코드에서 사용할 때 ToString, Equals 및 GetHashCode 메서드가 있는 것처럼 보이며 .NET 이러한 메서드에 대한 기본 동작을 제공합니다.