tags:

views:

427

answers:

3

Suppose you create a generic Object variable and assign it to a specific instance. If you do GetType(), will it get type Object or the type of the original class?

A: 

Yes.

You can also do:

object c = new FooBar();
if(c is FooBar)
     Console.WriteLine("FOOBAR!!!");
Alan
+2  A: 

Short answer: GetType() will return the Type of the specific object. I made a quick app to test this:

        Foo f = new Foo();
        Type t = f.GetType();

        Object o = (object)f;
        Type t2 = o.GetType();

        bool areSame = t.Equals(t2);

And yep, they are the same.

kurious
use: if(o is Foo) instead.
Alan
A: 

Calling GetType() will call the ACTUAL type. If you want to know the base type, you can call GetType().BaseType

Brian Genisio