ValueType.ToString Método
Definição
Retorna o nome do tipo totalmente qualificado dessa instância.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
Retornos
O nome de tipo totalmente qualificado.The fully qualified type name.
Comentários
O ValueType.ToString método substitui o Object.ToString método e fornece a implementação padrão do ToString método para tipos de valor.The ValueType.ToString method overrides the Object.ToString method and provides the default implementation of the ToString method for value types. (Os tipos de valor são tipos definidos pela struct palavra-chave em C# e pela Structure construção... End Structure em Visual Basic.) No entanto, funcionalmente a implementação é a mesma de Object.ToString : o método retorna o nome do tipo totalmente qualificado.(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.
Tipos de valor definidos pela struct palavra-chave em C# e a Structure construção... End Structure em Visual Basic normalmente substituem o ValueType.ToString método para fornecer uma representação de cadeia de caracteres mais significativa do tipo de valor.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. O exemplo a seguir ilustra a diferença.The following example illustrates the difference. Ele define dois tipos de valor EmployeeA e EmployeeB cria uma instância de cada um deles e chama seu ToString método.It defines two value types, EmployeeA and EmployeeB, creates an instance of each, and calls its ToString method. Como a EmployeeA estrutura não substitui o ValueType.ToString método, ela exibe somente o nome do tipo totalmente qualificado.Because the EmployeeA structure does not override the ValueType.ToString method, it displays only the fully qualified type name. O EmployeeB.ToString método, por outro lado, fornece informações significativas sobre o objeto.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
Observe que, embora os tipos de enumeração também sejam tipos de valor, eles derivam da Enum classe, que substitui ValueType.ToString .Note that, although enumeration types are also value types, they derive from the Enum class, which overrides ValueType.ToString.
Observações para o Windows RuntimeNotes for the Windows Runtime
Quando você chama o ToString método em uma estrutura de Windows Runtime, ele fornece o comportamento padrão para tipos de valor que não substituem ToString .When you call the ToString method on a Windows Runtime structure, it provides the default behavior for value types that don't override ToString. Isso faz parte do suporte que o .NET Framework fornece para o Windows Runtime (consulte .NET Framework suporte para aplicativos da Windows Store e Windows Runtime).This is part of the support that the .NET Framework provides for the Windows Runtime (see .NET Framework Support for Windows Store Apps and Windows Runtime). Estruturas de Windows Runtime não podem substituir ToString , mesmo que elas sejam escritas com C# ou Visual Basic, pois não podem ter métodos.Windows Runtime structures can't override ToString, even if they're written with C# or Visual Basic, because they can't have methods. (Além disso, as estruturas no Windows Runtime em si não herdam ValueType .) No entanto, eles parecem ToString ter Equals GetHashCode os métodos, e quando você os utiliza em seu código C# ou Visual Basic, e o .NET Framework fornece o comportamento padrão para esses métodos.(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 the .NET Framework provides the default behavior for these methods.