How is ToString implemented in System.Object


T

Tony Johansson

Hello!

I just curious how method ToString actually look like in System.Object. I
mean the actual code.
Does anyone have this kind of information or how it can look like.

Class Object
{
public virtual string ToString()
{

}
}

//Tony
 
Ad

Advertisements

A

Alberto Poblacion

Tony Johansson said:
I just curious how method ToString actually look like in System.Object. I
mean the actual code.
Does anyone have this kind of information or how it can look like.
I have not decompiled the actual code in the Framework, but it could
easily be something similar to this:

public class Object
{
public virtual string ToString()
{
return this.GetType().Name;
}
}
 
F

Family Tree Mike

Tony said:
Hello!

I just curious how method ToString actually look like in System.Object. I
mean the actual code.
Does anyone have this kind of information or how it can look like.

Class Object
{
public virtual string ToString()
{

}
}

//Tony
Perhaps like this, so that if ToString is not implimented in the base
class, that it ruturns the classname:
public virtual string ToString()
{
return this.GetType().ToString();
}
 
F

Family Tree Mike

Family said:
Perhaps like this, so that if ToString is not implimented in the base
class, that it ruturns the classname:
public virtual string ToString()
{
return this.GetType().ToString();
}
Doh! Alberto is correct, to use .Name() where I had .ToString().
 
Ad

Advertisements

T

Tony Johansson

Almost right I have checked it is
public class Object
{
public virtual string ToString()
{
return this.GetType().FullName;
}
}
//Tony
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Implement ToString on a method? 7
System.Object 8
ToString() 8
tostring 3
ToString() 6
ToString() 2
GetType().ToString() 6
When "ToString" does not equal "ToString" 11

Top