How is ToString implemented in System.Object

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
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
 
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;
}
}
 
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();
}
 
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().
 
Almost right I have checked it is
public class Object
{
public virtual string ToString()
{
return this.GetType().FullName;
}
}
//Tony
 
Back
Top