typeof() and .Gettype() in c#

Posted by krrishbiju-15589 under C# on 9/19/2013 | Points: 10 | Views : 2402 | Status : [Member] | Replies : 4
Hi,
Can anyone please help me on this,
1. What is typeof() and .Gettype() functions.
2.When we use this functions.
3.What is the difference between these two.

Regards
krrish




Responses

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
3. what is the difference between typeof(), GetType()

All are different.
1) typeof takes a type name (which you specify at compile time).
2) GetType gets the runtime type of an instance.
3) is returns true if an instance is in the inheritance tree.
Example
class Animal { } 

class Dog : Animal { }

void PrintTypes(Animal a) {
print(a.GetType() == typeof(Animal)) // false
print(a is Animal) // true
print(a.GetType() == typeof(Dog)) // true
}

Dog spot = new Dog();
PrintTypes(spot);


What about typeof(T)? Is it also resolved at compile time?

Yes. T is always what the type of the expression is. Remember, a generic method is basically a whole bunch of methods with the appropriate type. Example:

string Foo<T>(T object) { return typeof(T).Name; }


Animal probably_a_dog = new Dog();
Dog definitely_a_dog = new Dog();

Foo(probably_a_dog); // this calls Foo<Animal> and returns "Animal"
Foo<Animal>(probably_a_dog); // this is exactly the same as above
Foo<Dog>(probably_a_dog); // !!! This will not compile. The parameter expects a Dog, you cannot pass in an Animal.

Foo(definitely_a_dog); // this calls Foo<Dog> and returns "Dog"
Foo<Dog>(definitely_a_dog); // this is exactly the same as above.
Foo<Animal>(definitely_a_dog); // this calls Foo<Animal> and returns "Animal".
Foo((Animal)definitely_a_dog); // this does the same as above, returns "Animal"


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
2. When we use this functions.
You can only use typeof() when you know that type at compile time, and you're trying to obtain the corresponding Type object. (Although the type could be a generic type parameter, e.g. typeof(T) within a class with a type parameter T.) There don't need to be any instances of that type available to use typeof. The operand for typeof is always the name of a type or type parameter. It can't be a variable or anything like that.

Now compare that with object.GetType(). That will get the actual type of the object it's called on. This means:

You don't need to know the type at compile time (and usually you don't)
You do need there to be an instance of the type (as otherwise you have nothing to call GetType on)
The actual type doesn't need to be accessible to your code - for example, it could be an internal type in a different assembly
One odd point: GetType will give unexpected answers on nullable value types due to the way that boxing works. A call to GetType will always involve boxing any value type, including a nullable value type, and the boxed value of a nullable value type is either a null reference or a reference to an instance of a non-nullable value type


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
1.Waht is GetType()
GetType is a method on object. It provides a Type object, one that indicates the most derived type of the object instance. This program helps us understand the base class and derived class relationship.

Tip: Base classes are described with transitive closure. This affects the types allowed as variables. GetType returns a type pointer.
Program that uses GetType on base type instance: C#

using System;

class A
{
}

class B : A
{
}

class C : B
{
}

class Program
{
static void Main()
{
A a1 = new A();
A a2 = new B();
A a3 = new C();

Console.WriteLine(a1.GetType());
Console.WriteLine(a2.GetType());
Console.WriteLine(a3.GetType());
}
}

Output
---------
A
B
C


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
GetType gets resolved at runtime, while typeof is resolved at compile time.

typeof :
The typeof operator uses reflection to access the metadata descriptions of the types. The program then displays the string representation of these Type pointers. We assign the result of the typeof operator to a Type variable or field.

Program that uses typeof expressions: C#

using System;

using System.IO;

class Program
{
static Type _type = typeof(char); // Store Type as field

static void Main()
{
Console.WriteLine(_type); // Value type pointer
Console.WriteLine(typeof(int)); // Value type
Console.WriteLine(typeof(byte)); // Value type
Console.WriteLine(typeof(Stream)); // Class type
Console.WriteLine(typeof(TextWriter)); // Class type
Console.WriteLine(typeof(Array)); // Class type
Console.WriteLine(typeof(int[])); // Array reference type
}
}

Output
----------
System.Char
System.Int32
System.Byte
System.IO.Stream
System.IO.TextWriter
System.Array
System.Int32[]

Reference: http://msdn.microsoft.com/en-us/library/58918ffs.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response