How does the .ToString() method work?

后端 未结 3 1718
醉酒成梦
醉酒成梦 2020-12-06 13:43

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)

3条回答
  •  萌比男神i
    2020-12-06 14:18

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

    Correct.

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

    Correct.

    Does the System.Int32 struct override the ToString method?

    Yes.

    Do other types whose ToString methods return the fully-qualified type name not override 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 ToString just calls the System.Object implementation of ToString, which returns fully qualified name?

    Correct.

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

提交回复
热议问题