How to get a class name of an object?

  • Thread starter Thread starter K Viltersten
  • Start date Start date
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...
 
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";
}
 
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

Back
Top