Monday, October 20, 2008

Finding types (GetType,Is, typeof)

1. How do we determine the type of an object ?

MyProject.Customer customer1= new MyProject.Customer { name= "sree" ,age ="22"} ;

//Declare Type instance
Type t1 = null;
t1 = customer1.GetType();

2. How do we check if an object customer1 is of type Customer ?
is operator - if(customer1 is MyProject.Customer)

3. How to get Type information from a type and not an instance of type (object)
t1 = typeof(MyProject.Customer);

Note:- typeof() is an operator and GetType() is a method defined in System.Object an both of them returns and object of System.Type


-Sree

No comments: