I have read that ToString() uses reflection (although it should just put quotation marks around the object it is called on so I don’t know where/why it may use reflection). Is there any proof of this? And is there any performance penalty of calling .ToString() (maybe I should call Convert.ToString()?)?
I have read that ToString() uses reflection (although it should just put quotation marks
Share
If you don’t override the
ToString()method, you end up callingObject.ToString(), whose implementation looks something like this:The method doesn’t simply “put quotation marks around the object it is called on”. In order to accomplish that result, it must reflectively get the runtime type information and call
ToString()on that, which in turn looks something like this:Anytime you go through reflection for something there is a bit of a performance penalty, but in this case probably not one large enough to really matter – unless you are doing it inside of a very tight, very large loop.
Are you seeing a performance problem? What are you trying to accomplish that is leading you to want to call
Object.ToString()rather than calling an overridden version that you provide?