ValueType.ToString 메서드

정의

이 인스턴스의 정규화된 형식 이름을 반환합니다.

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 기본 구현을 제공합니다. 값 형식은 에서 정의한 형식입니다struct. C#의 키워드(keyword)...StructureEnd Structure Visual Basic의 구문입니다.) 그러나 기능적으로 구현은 의 Object.ToString구현과 동일합니다. 메서드는 정규화된 형식 이름을 반환합니다.

C#의 키워드(keyword) 정의한 값 형식 structStructureVisual Basic의 ...End Structure 구문은 일반적으로 메서드를 재정 ValueType.ToString 의하여 값 형식의 보다 의미 있는 문자열 표현을 제공합니다. 다음 예제에서 차이점을 보여 줍니다. 두 가지 값 형식을 정의하고 EmployeeBEmployeeA 값의 instance 만들고 메서드를 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 코드에서 사용할 때 , EqualsGetHashCode 메서드가 있는 것처럼 보이고 ToString.NET은 이러한 메서드에 대한 기본 동작을 제공합니다.

적용 대상