473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Difference between is and .GetType() == typeof(class)

What is the difference between doing this:

if (myClass is Class)
{
//Do something here
}

and this?:

if (myClass.GetType() == typeof(Class))
{
//Do something here
}

Because in some cases the first expression evaluates to false but the
second expression remains true. When this happens I can always cast
myClass to the type Class.

What I´m missing here?

Thanks in advance

May 2 '07 #1
5 63601
Hi,
It's not exactly the same, and the problem appears when you use
inheritance.
ie: WebPage1 inherits from Page, and this one inherits also from
Object, so if you test for (new WebPage1()).GetType() ==
typeof(object) it'll return false because the types are diferent, but
when you test using the IS operator it's true.
((new WebPage1()) is object) is true because (new WebPage1()) is an
object of type WebPage1, and algo a Page and an object.
The types might be different, but IS checks if you can cast safely to
this type.

Bye,
Diego

May 2 '07 #2
iCeCLoW <ic*****@gmail.comwrote:
What is the difference between doing this:

if (myClass is Class)
{
//Do something here
}

and this?:

if (myClass.GetType() == typeof(Class))
{
//Do something here
}

Because in some cases the first expression evaluates to false but the
second expression remains true.
Could you post a short but complete program which demonstrates this?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

I can see how the opposite would happen, just by virtue of derived
classes where myClass refers to an instance of a class derived from
Class - but I can't see why the first would be false and the second
true.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 2 '07 #3
Jon Skeet [C# MVP] <sk***@pobox.comwrote:

<snip>
I can see how the opposite would happen, just by virtue of derived
classes where myClass refers to an instance of a class derived from
Class - but I can't see why the first would be false and the second
true.
Ooh - I've worked out a nasty bit of code where it would be the way
round you said. It's dirty though:

using System;

class EvilType
{
public new Type GetType()
{
return typeof(InnocentType);
}
}

class InnocentType
{
}

class Test
{
static void Main()
{
EvilType evil = new EvilType();
Console.WriteLine ("evil is InnocentType? {0}",
evil is InnocentType);
Console.WriteLine ("evil.GetType()==typeof(InnocentType)? {0}",
evil.GetType()==typeof(InnocentType));
}
}

The cast would also succeed if you had an explicit conversion from
EvilType to InnocentType.

I feel like I ought to have a bath after writing that...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 2 '07 #4
On 2 May 2007 15:20:56 -0700, iCeCLoW <ic*****@gmail.comwrote:
>Because in some cases the first expression evaluates to false but the
second expression remains true. When this happens I can always cast
myClass to the type Class.
Diego is correct. The "is" operator tests for type _compatibility_
(basically Liskov substitutability [1]) whereas the comparison GetType
== typeof tests for type _identity_.

Type A is identical with type B only if both are the exact same type.

But A is compatible with B if A is the same type or a derived type.
That includes interface implementations, e.g. if you declare class A:
IDisposable then the test (A is IDisposable) will succeed.

[1] http://en.wikipedia.org/wiki/Liskov_...tion_principle
--
http://www.kynosarges.de
May 3 '07 #5
On Thu, 3 May 2007 00:05:34 +0100, Jon Skeet [C# MVP]
<sk***@pobox.comwrote:
>iCeCLoW <ic*****@gmail.comwrote:
>>
Because in some cases the first expression evaluates to false but the
second expression remains true.

I can see how the opposite would happen, just by virtue of derived
classes where myClass refers to an instance of a class derived from
Class - but I can't see why the first would be false and the second
true.
Well spotted, I missed that when I first read the original post. I'm
pretty sure it's just a typo, though, and he did mean to say the
opposite. Without horrible contortions as in your example, the "is"
comparison always succeeds where the GetType comparison does.
--
http://www.kynosarges.de
May 3 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: audipen | last post by:
I have a problem with System.Type.GetType method. If you try out the following code in C# console app .. System.Type t = System.Type.GetType("System.DateTime"); System.Type t1 =...
2
by: Baccarin | last post by:
Hello C# gurus, I would like your opinion. Who is quicker to obtain a System.Type(): typeof() or System.Type.GetType()? Regards, Baccarin.
8
by: ron | last post by:
Hi, I am currently using the base class GetType() in the following way and was woundering if there was a different way of looking at types and doing a comparison for equality. Thanks Ron ...
7
by: .pd. | last post by:
Hello, I'm trying to setup a named-object list using objects of 3 different types all derived from the same base class. public class namedObjectList :...
2
by: Claus Konrad | last post by:
Hi Any thoughtfull insight at to whether a difference exists between the two functions mentioned above? I am just wondering when to use the one over the other...?? Thanks! /Claus
3
by: srkvellanki | last post by:
Hi all, Please give me the differences between typeof and GetType bye
3
by: Fredrik Wahlgren | last post by:
Hi I found an interesting piece of code which shows how to makee an Excel automation add-in at http://www.codeproject.com/dotnet/excelnetauto.asp. I decide to experiment with it and i made a few...
4
by: Greg Burns | last post by:
What the difference between these two? System.Type.GetType("System.Int32") and GetType(Integer) Or more specifically, why does GetType(Integer) work, but not System.Type.GetType(Integer)?...
2
by: doker0 | last post by:
Hi. Recently, when I was designing myown buttonbar and a designer serialization(typeconverter) for its button collection I've slipped on construction like this: If TypeOf value Is Field Then ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.