difference between gettype() and typeof()?

Posted by Santosh198318 under C# on 6/12/2011 | Points: 10 | Views : 8394 | Status : [Member] | Replies : 3
difference between gettype() and typeof()? please provide with example?




Responses

Posted by: SheoNarayan on: 6/12/2011 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
HI Santosh,

Read this Interview question post http://www.dotnetfunda.com/interview/exam3620-what-is-the-difference-between-gettype-and-typeof-.aspx to know about the difference.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Santosh198318, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakn2 on: 6/13/2011 [Member] Starter | Points: 25

Up
0
Down
Trivially, whereas GetType operates on an object, typeof operates on a type. But, notice that GetType returns the underlying type of the object which can be different from the type of the reference to the object. For instance:

class BaseClass {}


class DerivedClass: BaseClass {}

class Example
{
static void RevealType (BaseClass baseClass)
{
Console.WriteLine (typeof(BaseClass));
Console.WriteLine (baseClass.GetType());
}

static void Main()
{
RevealType (new DerivedClass());
}
}

gives the following output:




Example output (program output)
BaseClass
DerivedClass




Thanks&Regards
LakshmiNarayana Nalluri.

Santosh198318, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 6/13/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

Refer this link,

http://en.csharp-online.net/CSharp_FAQ:_What_is_the_difference_between_GetType_and_typeof

Regards,
Susan

Santosh198318, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response