C# .ToString() 方法是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10075751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:21:22  来源:igfitidea点击:

How does the .ToString() method work?

c#tostringobject-to-string

提问by deen

Sometimes when I call a class's .ToString()method, it returns the fully qualified name of the class. But for some class's/struct's (like Int32) it returns a string correspoding to the object (value of the integer). Does this mean the Int32class overrides the ToString()method, and classes that return fully qualified names don't override it, but instead just call base's (Object's) ToString()method? Does the Object.ToString()implementation just return the class's fully qualified name?

有时,当我调用类的.ToString()方法时,它返回类的完全限定名称。但是对于某些类的/结构(如Int32),它返回一个对应于对象的字符串(整数的值)。这是否意味着Int32该类会覆盖该ToString()方法,并且返回完全限定名称的类不会覆盖它,而只是调用 base 的 ( Object's)ToString()方法?Object.ToString()实现是否只返回类的完全限定名称?

采纳答案by Eric Lippert

Sometimes when I call the ToStringmethod it returns the fully qualified name of the runtime type of the object that received the call.

有时,当我调用该ToString方法时,它会返回接收调用的对象的运行时类型的完全限定名称。

Correct.

正确的。

But for some types, such as System.Int32, ToStringreturns the value of the receiver converted to a string.

但是对于某些类型,例如System.Int32ToString返回接收器的值转换为字符串。

Correct.

正确的。

Does the System.Int32struct override the ToStringmethod?

System.Int32结构是否覆盖了ToString方法?

Yes.

是的。

Do other types whose ToStringmethods return the fully-qualified type name notoverride ToString?

ToString方法返回完全限定类型名称的其他类型不会覆盖ToString吗?

That is probably the case, yes. Of course, they could override the method and have the overriding method do exactly the same thing as the base class method, but that would be a bit pointless.

情况大概就是这样,是的。当然,他们可以覆盖该方法并使覆盖方法与基类方法做完全相同的事情,但这有点毫无意义。

So in those cases, calling ToStringjust calls the System.Objectimplementation of ToString, which returns fully qualified name?

那么在这些情况下,调用ToString只是调用 的System.Object实现ToString,它返回完全限定名称?

Correct.

正确的。

You seem to have a solid grasp of how this works. My only correction would be to note that System.Int32is a struct, not a class.

你似乎对这是如何工作的有一个很好的理解。我唯一的更正是要注意它System.Int32是一个struct,而不是一个class

回答by MarcinJuraszek

Have you even tried to search for answer to your question?

您是否尝试过寻找问题的答案?

http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx

http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx

ToString is the major formatting method in the .NET Framework. It converts an object to its string representation so that it is suitable for display. (For information about formatting support in the .NET Framework, see Formatting Types.)

The default implementation of the ToString method returns the fully qualified name of the type of the Object, as the following example shows.

Because Object is the base class of all reference types in the .NET Framework, this behavior is inherited by reference types that do not override the ToString method. The following example illustrates this. It defines a class named Object1 that accepts the default implementation of all Object members. Its ToString method returns the object's fully qualified type name.

ToString 是 .NET Framework 中的主要格式化方法。它将对象转换为其字符串表示形式,以便适合显示。(有关 .NET Framework 中的格式支持的信息,请参阅格式类型。)

ToString 方法的默认实现返回对象类型的完全限定名称,如以下示例所示。

由于 Object 是 .NET Framework 中所有引用类型的基类,因此此行为由不重写 ToString 方法的引用类型继承。以下示例说明了这一点。它定义了一个名为 Object1 的类,该类接受所有 Object 成员的默认实现。它的 ToString 方法返回对象的完全限定类型名称。

回答by Vivek Kumar Singh

Few points regarding the ToString() method in C#.

关于 C# 中 ToString() 方法的几点。

  1. ToString() method is defined in the base System.Object class and hence its available for all the types and parameters to use.

  2. The default implementation of ToString() that is provided by the system.object base class will give you the complete name of the type including the namespace.

  3. If you don't want the default implementation, then you can override the ToString() method. Yes ToString() method is overridable. And where do you override it? You override it in the class where you don't want its default implementation.

  1. ToString() 方法在基本 System.Object 类中定义,因此它可用于所有类型和参数使用。

  2. system.object 基类提供的 ToString() 的默认实现将为您提供类型的完整名称,包括命名空间。

  3. 如果您不想要默认实现,则可以覆盖 ToString() 方法。是的 ToString() 方法是可覆盖的。你在哪里覆盖它?您可以在不想要其默认实现的类中覆盖它。