DotNet FrameWork – What is the difference between typeof(foo) and myFoo.GetType() – Day 28 of 30

What is the difference between typeof(foo) and myFoo.GetType()?

Typeof is operator which applied to a object returns System.Type object. Typeof cannot be overloaded white GetType has lot of overloads.GetType is a method which also returns System.Type of an object. GetType is used to get the runtime type of the object.

Example from MSDN showing Gettype used to retrive type at runtime:-

public class MyBaseClass: Object {   ………………..   }

public class MyDerivedClass: MyBaseClass {  ………………   }

public class Test {

public static void Main() {

MyBaseClass myBase = new MyBaseClass();

MyDerivedClass myDerived = new MyDerivedClass();

object o = myDerived;

MyBaseClass b = myDerived;

Console.WriteLine(“mybase: Type is {0}”, myBase.GetType());

Console.WriteLine(“myDerived: Type is {0}”, myDerived.GetType());

Console.WriteLine(“object o = myDerived: Type is {0}”, o.GetType());

Console.WriteLine(“MyBaseClass b = myDerived: Type is {0}”, b.GetType());

}

}

/*

This code produces the following output.

mybase: Type is MyBaseClass

myDerived: Type is MyDerivedClass

object o = myDerived: Type is MyDerivedClass

MyBaseClass b = myDerived: Type is MyDerivedClass

*/

Can “this” be used within a static method?

No ‘This’ cannot be used in a static method. As only static variables/methods can be used in a static method.

Please read all the post in the Dotnet Framework series.

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s