What is the difference between GetType and TypeOf?

 Posted by Tripati_tutu on 12/20/2010 | Category: ASP.NET Interview questions | Views: 7787 | Points: 40
Answer:

The GetType function, which is used to get the type of an object based on that object that is the instance of that class. It means that, this function needs parameter as an argument of object rather than class name.

But in case of typeOf , we have to pass that class name as a parameter. This function is used to get the type based on a class. That is suppose you will use typeOf function with an object as a argument, then it will give you error.

Example-
string str = “Hello”;

Type val1 = str.GetType();
Type val2 = typeof(string);


Here in the above example in both cases you will get output as true.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Premkand on: 3/3/2011 | Points: 10
Hi,

The return value is not true. It returns the class name, in this case, it returns System.String on both.


Thanks,

Prem

Login to post response