473,395 Members | 1,343 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,395 software developers and data experts.

Help with the 'is' command...

Can someone tell me how the 'is' command works in relation to derived classes?

For example -
Class A {}
Class B : A {}

object o = new B();

if (o is A) {...}

Should the above if statement return true or false?

Thanx.
Nov 15 '05 #1
12 1225
On Wed, 19 Nov 2003 08:01:04 -0800, "curious1"
<an*******@discussions.microsoft.com> wrote:
Can someone tell me how the 'is' command works in relation to derived classes?

For example -
Class A {}
Class B : A {}

object o = new B();

if (o is A) {...}

Should the above if statement return true or false?

Thanx.


My vote would be false, it's not A - it's B.

Simple enough to try though, what results do you get if you put a
small test app together?

--
Jeff Gaines Damerham Hampshire UK

Nov 15 '05 #2
Sorry all,

My own fault for not actually deriving the class from the original (forgot the :A in the definition of B).

Thanx again.
Nov 15 '05 #3
Jeff Gaines <je**@jgaines.co.uk> wrote:
On Wed, 19 Nov 2003 08:01:04 -0800, "curious1"
<an*******@discussions.microsoft.com> wrote:
Can someone tell me how the 'is' command works in relation to derived classes?

For example -
Class A {}
Class B : A {}

object o = new B();

if (o is A) {...}

Should the above if statement return true or false?

Thanx.


My vote would be false, it's not A - it's B.


No, the correct answer is true, because it *is* an A in the same way
that a String is also an Object.

See section 14.9.9 of the ECMA C# spec for more details.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

"Jeff Gaines" <je**@jgaines.co.uk> wrote in message
news:b5********************************@4ax.com...
On Wed, 19 Nov 2003 08:01:04 -0800, "curious1"
<an*******@discussions.microsoft.com> wrote:
Can someone tell me how the 'is' command works in relation to derived classes?

For example -
Class A {}
Class B : A {}

object o = new B();

if (o is A) {...}

Should the above if statement return true or false?

Thanx.


My vote would be false, it's not A - it's B.

Simple enough to try though, what results do you get if you put a
small test app together?

--
Jeff Gaines Damerham Hampshire UK


Actually, it is not B. It is not A either. It is an instance of B which derives
from A so "o = A" and "o = B". BUT, if you have:

Class A {}
Class B:A{}

object a = new A();
object b = new B();

then

a = A;
a != B;
b = A;
b = B;

get it?

Off top of my head using my own thinking process, which is 50% correct .. or not.

Mythran
Nov 15 '05 #5
100
Hi curious1,

Answer is *true*
o is B and B is A so o is A as well

You can try it.

B\rgds
100
"curious1" <an*******@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
Can someone tell me how the 'is' command works in relation to derived classes?
For example -
Class A {}
Class B : A {}

object o = new B();

if (o is A) {...}

Should the above if statement return true or false?

Thanx.

Nov 15 '05 #6
Jeff Gaines wrote:
On Wed, 19 Nov 2003 08:01:04 -0800, "curious1"
<an*******@discussions.microsoft.com> wrote:

Can someone tell me how the 'is' command works in relation to derived classes?

For example -
Class A {}
Class B : A {}

object o = new B();

if (o is A) {...}

Should the above if statement return true or false?

Thanx.

My vote would be false, it's not A - it's B.

Simple enough to try though, what results do you get if you put a
small test app together?


I'm pretty sure it is true. One would expect A is System.Object to be
true for example..... because it is :)

Nov 15 '05 #7
Of course it will return true.

Why don't you try it live? I don't really understand people who post
questions that can be tested with a 2 line program (that they actually write
when they post their questions!). Also, looking at the reference manual for
the "is keyword" will give you the answer (and a small source example with
the output that it produces, so you don't even have to compile and run it).

Bruno
"curious1" <an*******@discussions.microsoft.com> a écrit dans le message de
news:0B**********************************@microsof t.com...
Can someone tell me how the 'is' command works in relation to derived classes?
For example -
Class A {}
Class B : A {}

object o = new B();

if (o is A) {...}

Should the above if statement return true or false?

Thanx.

Nov 15 '05 #8
Bruno Jouhier [MVP] wrote:
Of course it will return true.

Why don't you try it live? I don't really understand people who post
questions that can be tested with a 2 line program (that they actually write
when they post their questions!). Also, looking at the reference manual for
the "is keyword" will give you the answer (and a small source example with
the output that it produces, so you don't even have to compile and run it).

Bruno

Amen to that - and even worse, you get several posts from people
*guessing* the answer!


"curious1" <an*******@discussions.microsoft.com> a écrit dans le message de
news:0B**********************************@microsof t.com...
Can someone tell me how the 'is' command works in relation to derived


classes?
For example -
Class A {}
Class B : A {}

object o = new B();

if (o is A) {...}

Should the above if statement return true or false?

Thanx.



--
mikeb

Nov 15 '05 #9
Related to subject:

I was reading an MS developer's blog a little while back, and they state
that if you are going to use "is" in perparation to do a cast, it's better
performance and better form to use the "as" operator and test for null.

So, they're saying that instead of doing:
if (someObject is A)
{
A anA = (A)someObject;
...
}

That we should do this:
A anA = someObject as A;
if(anA != null)
{
}

The reasont that the blogger gave is that the first example, using "is"
actually performs 2 casts, one for the is and then the "real" cast.

The "as" example only performs one cast, and by using "as" instead of "()",
no exceptions are thrown.

"curious1" <an*******@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
Can someone tell me how the 'is' command works in relation to derived classes?
For example -
Class A {}
Class B : A {}

object o = new B();

if (o is A) {...}

Should the above if statement return true or false?

Thanx.

Nov 15 '05 #10
Hi again. I dug up the actual blog post. The post was made by Brad Abrams:
(watch for linewrap)
http://blogs.gotdotnet.com/BradA/per...7-da18088cd8f0

"J.Marsch" <je****@ctcdeveloper.com> wrote in message
news:O4**************@TK2MSFTNGP09.phx.gbl...
Related to subject:

I was reading an MS developer's blog a little while back, and they state
that if you are going to use "is" in perparation to do a cast, it's better
performance and better form to use the "as" operator and test for null.

So, they're saying that instead of doing:
if (someObject is A)
{
A anA = (A)someObject;
...
}

That we should do this:
A anA = someObject as A;
if(anA != null)
{
}

The reasont that the blogger gave is that the first example, using "is"
actually performs 2 casts, one for the is and then the "real" cast.

The "as" example only performs one cast, and by using "as" instead of "()", no exceptions are thrown.

"curious1" <an*******@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
Can someone tell me how the 'is' command works in relation to derived

classes?

For example -
Class A {}
Class B : A {}

object o = new B();

if (o is A) {...}

Should the above if statement return true or false?

Thanx.


Nov 15 '05 #11
100
Hi J.Marsch,
Both operator *as* and *is* are translated to the same IL instruction
*isinst*.
*isinst* instruction pushes the reference to the requested type or null of
the cast is incorrect on the top of the evaluation stack.
Here comes the diference between *as* and *is* instructions. Code generated
for the *as* operator just saves the reference in a variable; Code for the
*is* instruction compares the result with *null*

What I want ot say is that *is* instruction actually does the casting.
So doing
if(a is B)
{
b = (B)a;
}

Actually you do casting two times. For b = (B) the compiler emits
*castclass* instruction which is the same as *isinst* with the only
difference that the exception is thrown if the cast is imposible instead of
pushing *null* in the evaluation stack.

IMHO *is* should be used in the rare cases (I think) where you won't do
casting afterwards.

B\rgds
100

"J.Marsch" <je****@ctcdeveloper.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi again. I dug up the actual blog post. The post was made by Brad Abrams: (watch for linewrap)
http://blogs.gotdotnet.com/BradA/per...7-da18088cd8f0
"J.Marsch" <je****@ctcdeveloper.com> wrote in message
news:O4**************@TK2MSFTNGP09.phx.gbl...
Related to subject:

I was reading an MS developer's blog a little while back, and they state
that if you are going to use "is" in perparation to do a cast, it's better performance and better form to use the "as" operator and test for null.

So, they're saying that instead of doing:
if (someObject is A)
{
A anA = (A)someObject;
...
}

That we should do this:
A anA = someObject as A;
if(anA != null)
{
}

The reasont that the blogger gave is that the first example, using "is"
actually performs 2 casts, one for the is and then the "real" cast.

The "as" example only performs one cast, and by using "as" instead of

"()",
no exceptions are thrown.

"curious1" <an*******@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
Can someone tell me how the 'is' command works in relation to derived

classes?

For example -
Class A {}
Class B : A {}

object o = new B();

if (o is A) {...}

Should the above if statement return true or false?

Thanx.



Nov 15 '05 #12
Makes perfect sense: the first form will test the cast twice, the second one
will test it only once. This is the costly part. Testing against null is
almost no cost.

Bruno.

"J.Marsch" <je****@ctcdeveloper.com> a écrit dans le message de
news:O4**************@TK2MSFTNGP09.phx.gbl...
Related to subject:

I was reading an MS developer's blog a little while back, and they state
that if you are going to use "is" in perparation to do a cast, it's better
performance and better form to use the "as" operator and test for null.

So, they're saying that instead of doing:
if (someObject is A)
{
A anA = (A)someObject;
...
}

That we should do this:
A anA = someObject as A;
if(anA != null)
{
}

The reasont that the blogger gave is that the first example, using "is"
actually performs 2 casts, one for the is and then the "real" cast.

The "as" example only performs one cast, and by using "as" instead of "()", no exceptions are thrown.

"curious1" <an*******@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
Can someone tell me how the 'is' command works in relation to derived

classes?

For example -
Class A {}
Class B : A {}

object o = new B();

if (o is A) {...}

Should the above if statement return true or false?

Thanx.


Nov 15 '05 #13

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

Similar topics

5
by: Phil Powell | last post by:
How do I suppress the lines PHP normally delivers to stdout (your browser) if I am doing command-line PHP? e.g. stuff.php: <? echo 'Hello World'; ?> Calling it from a Red Hat 7.3...
3
by: aToaster | last post by:
Hey guys, I'm just getting the hang of Python and Tkinter and I could use some help. I wrote most of the gui for my calculator program, well I haven't gotten around to putting in the...
7
by: Bill Kellaway | last post by:
Hi there - this should be fairly simple for someone. Basically I can't figure out how to pass the parameters from ASP to a Stored Procedure on SQL. Here's my code: I just need to help in...
9
by: Rob | last post by:
I am trying to write a program with VC++ 6.0 to read a txt file which looks like this: Network Destination Netmask Gateway Interface Metric 0.0.0.0 0.0.0.0 ...
2
by: meyvn77 | last post by:
I would like to know if it is possible to start a job from a stored procedure? I have a DTS that I set as a job and would like to either call it from an ADP with Conn.Execute "EXEC...
3
by: Merdelus | last post by:
I'm a new visual basic learner, I need some help with this program below: create an application that either sums or averagethe rows or columns of a two dimensional array depending on user...
2
by: Merdelus | last post by:
I'm a new visual basic learner, I need some help with this program below: create an application that either sums or averagethe rows or columns of a two dimensional array depending on user...
0
by: TiNo | last post by:
Hi, I'm having problems installing easy_install. When I run python ez_setup.py I get: G:\python>python ez_setup.py 'import site' failed; use -v for traceback Downloading...
1
by: Kenichi666 | last post by:
I am new here - and new to the exciting world of programming. I want to Update a table via a dropdownlistbox that contains the ID's. So when you select the ID - it should update that selected...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.