How to get a class name of an object?

K

K Viltersten

Normally, when one prints out an object,
ToString() is called and it contains the name
of the class that the object is of.

Now, i'd like to override it, so that instead
of it returning
"ThatCoolClass"
it will return
"An instance of ThatCoolClass class"
but i don't know how to obtain the name...
 
A

Anthony Jones

K Viltersten said:
Normally, when one prints out an object,
ToString() is called and it contains the name
of the class that the object is of.

Now, i'd like to override it, so that instead
of it returning
"ThatCoolClass"
it will return
"An instance of ThatCoolClass class"
but i don't know how to obtain the name...

This is one option:-

public override string ToString()
{
return "An instance of " + GetType().Name +" class";
}
 
K

K Viltersten

Normally, when one prints out an object,
This is one option:-

public override string ToString()
{
return "An instance of " + GetType().Name +" class";
}

Ah, System.Type seems to be a very nice tool
in my case. Thanks!
 

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

about the hashtable and equals 11
inheritance 4
Using Queue with multiple class objects 3
Java to C#. Please help. 3
hashCode understanding 5
tostring 3
Function Pointer in Class 2
Wrapper class template 1

Top