Help with the 'is' command...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • curious1

    #1

    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.


  • Jeff Gaines

    #2
    Re: Help with the 'is' command...

    On Wed, 19 Nov 2003 08:01:04 -0800, "curious1"
    <anonymous@disc ussions.microso ft.com> wrote:
    [color=blue]
    >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.
    >[/color]

    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

    Comment

    • bonehead1

      #3
      RE: Help with the 'is' command...

      Sorry all,

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

      Thanx again.


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Help with the 'is' command...

        Jeff Gaines <jeff@jgaines.c o.uk> wrote:[color=blue]
        > On Wed, 19 Nov 2003 08:01:04 -0800, "curious1"
        > <anonymous@disc ussions.microso ft.com> wrote:
        >[color=green]
        > >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.
        > >[/color]
        >
        > My vote would be false, it's not A - it's B.[/color]

        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 - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Mythran

          #5
          Re: Help with the 'is' command...


          "Jeff Gaines" <jeff@jgaines.c o.uk> wrote in message
          news:b56nrvo1mi s85gpuu8sbl1knj i8uee7pao@4ax.c om...[color=blue]
          > On Wed, 19 Nov 2003 08:01:04 -0800, "curious1"
          > <anonymous@disc ussions.microso ft.com> wrote:
          >[color=green]
          > >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.
          > >[/color]
          >
          > 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
          >[/color]

          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


          Comment

          • 100

            #6
            Re: Help with the 'is' command...

            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" <anonymous@disc ussions.microso ft.com> wrote in message
            news:0B5C0ABE-7FF9-4BD8-B251-7BA62BE134D3@mi crosoft.com...[color=blue]
            > Can someone tell me how the 'is' command works in relation to derived[/color]
            classes?[color=blue]
            >
            > 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.
            >
            >[/color]


            Comment

            • Paul Robson

              #7
              Re: Help with the 'is' command...

              Jeff Gaines wrote:
              [color=blue]
              > On Wed, 19 Nov 2003 08:01:04 -0800, "curious1"
              > <anonymous@disc ussions.microso ft.com> wrote:
              >
              >[color=green]
              >>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.
              >>[/color]
              >
              >
              > 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?
              >[/color]

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

              Comment

              • Bruno Jouhier [MVP]

                #8
                Re: Help with the 'is' command...

                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" <anonymous@disc ussions.microso ft.com> a écrit dans le message de
                news:0B5C0ABE-7FF9-4BD8-B251-7BA62BE134D3@mi crosoft.com...[color=blue]
                > Can someone tell me how the 'is' command works in relation to derived[/color]
                classes?[color=blue]
                >
                > 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.
                >
                >[/color]


                Comment

                • mikeb

                  #9
                  Re: Help with the 'is' command...

                  Bruno Jouhier [MVP] wrote:
                  [color=blue]
                  > 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
                  >[/color]

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

                  [color=blue]
                  >
                  > "curious1" <anonymous@disc ussions.microso ft.com> a écrit dans le message de
                  > news:0B5C0ABE-7FF9-4BD8-B251-7BA62BE134D3@mi crosoft.com...
                  >[color=green]
                  >>Can someone tell me how the 'is' command works in relation to derived[/color]
                  >
                  > classes?
                  >[color=green]
                  >>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.
                  >>
                  >>[/color]
                  >
                  >
                  >[/color]

                  --
                  mikeb

                  Comment

                  • J.Marsch

                    #10
                    Re: Help with the 'is' command... (perf issue)

                    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" <anonymous@disc ussions.microso ft.com> wrote in message
                    news:0B5C0ABE-7FF9-4BD8-B251-7BA62BE134D3@mi crosoft.com...[color=blue]
                    > Can someone tell me how the 'is' command works in relation to derived[/color]
                    classes?[color=blue]
                    >
                    > 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.
                    >
                    >[/color]


                    Comment

                    • J.Marsch

                      #11
                      Re: Help with the 'is' command... (perf issue)

                      Hi again. I dug up the actual blog post. The post was made by Brad Abrams:
                      (watch for linewrap)




                      "J.Marsch" <jeremy@ctcdeve loper.com> wrote in message
                      news:O4OMpKtrDH A.1872@TK2MSFTN GP09.phx.gbl...[color=blue]
                      > 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[/color]
                      "()",[color=blue]
                      > no exceptions are thrown.
                      >
                      > "curious1" <anonymous@disc ussions.microso ft.com> wrote in message
                      > news:0B5C0ABE-7FF9-4BD8-B251-7BA62BE134D3@mi crosoft.com...[color=green]
                      > > Can someone tell me how the 'is' command works in relation to derived[/color]
                      > classes?[color=green]
                      > >
                      > > 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.
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      • 100

                        #12
                        Re: Help with the 'is' command... (perf issue)

                        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" <jeremy@ctcdeve loper.com> wrote in message
                        news:%23ITLnPtr DHA.2360@TK2MSF TNGP09.phx.gbl. ..[color=blue]
                        > Hi again. I dug up the actual blog post. The post was made by Brad[/color]
                        Abrams:[color=blue]
                        > (watch for linewrap)
                        >[/color]
                        http://blogs.gotdotnet.com/BradA/per...7-da18088cd8f0[color=blue]
                        >
                        >
                        >
                        > "J.Marsch" <jeremy@ctcdeve loper.com> wrote in message
                        > news:O4OMpKtrDH A.1872@TK2MSFTN GP09.phx.gbl...[color=green]
                        > > 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[/color][/color]
                        better[color=blue][color=green]
                        > > 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[/color]
                        > "()",[color=green]
                        > > no exceptions are thrown.
                        > >
                        > > "curious1" <anonymous@disc ussions.microso ft.com> wrote in message
                        > > news:0B5C0ABE-7FF9-4BD8-B251-7BA62BE134D3@mi crosoft.com...[color=darkred]
                        > > > Can someone tell me how the 'is' command works in relation to derived[/color]
                        > > classes?[color=darkred]
                        > > >
                        > > > 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.
                        > > >
                        > > >[/color]
                        > >
                        > >[/color]
                        >
                        >[/color]


                        Comment

                        • Bruno Jouhier [MVP]

                          #13
                          Re: Help with the 'is' command... (perf issue)

                          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" <jeremy@ctcdeve loper.com> a écrit dans le message de
                          news:O4OMpKtrDH A.1872@TK2MSFTN GP09.phx.gbl...[color=blue]
                          > 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[/color]
                          "()",[color=blue]
                          > no exceptions are thrown.
                          >
                          > "curious1" <anonymous@disc ussions.microso ft.com> wrote in message
                          > news:0B5C0ABE-7FF9-4BD8-B251-7BA62BE134D3@mi crosoft.com...[color=green]
                          > > Can someone tell me how the 'is' command works in relation to derived[/color]
                          > classes?[color=green]
                          > >
                          > > 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.
                          > >
                          > >[/color]
                          >
                          >[/color]


                          Comment

                          Working...