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
 
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().
 
T

Tony Johansson

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

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

Top