Richard77
Richard77

Reputation: 21621

How do I get the type (not of a class but that) of an instance of class

To get the type of a class called myClass I do this

Type myType = (typeof(myClass))

How about, instead of having myClass, I've rather the instanceOfMyClass? How do I get the type?

Let's say I've this

Contact contact = new Contact();

If I want to know which type is contact, how do I do it

Thanks for helping

Upvotes: 1

Views: 82

Answers (2)

alastairs
alastairs

Reputation: 6805

If I've read this correctly, you can get the run-time type of the object via:

Contact contact = new Contact();
Type contactType = contact.GetType();

Upvotes: 2

João Angelo
João Angelo

Reputation: 57658

You use contact.GetType(); method. The GetType is defined in the Object class so is available globally.

Upvotes: 4

Related Questions