Overloading operator== and comparing to null

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

    #1

    Overloading operator== and comparing to null

    Not sure how I've not come across this before...

    I've overloaded operator == for one of my classes using
    the following code.

    public static bool operator ==(Stock x, Stock y)
    {
    return x.Equals(y);
    }
    public override bool Equals(object obj)
    {
    Stock queryObject = null;
    if (obj is Stock)
    queryObject = (Stock) obj;
    else
    return false;

    return ((queryObject.S tockCode == this.StockCode) );
    }

    In another class I have the following code:

    private Stock _stock = null;
    private Stock getStock()
    {
    if (_stock == null) // Error here...
    {
    _stock = new Stock()
    // populate details
    }
    }

    My error occurs, because I initialise my local copy to
    null, so it has no methods...
    How do I check whether or not my object is null??

    Thanks in advance
    Paul
  • Jack Hanebach

    #2
    Re: Overloading operator== and comparing to null

    "Paul Lyons" <paul@the-lyons.removethi s.com> wrote in message
    news:0eae01c356 b9$be407660$a50 1280a@phx.gbl.. .
    [color=blue]
    > public static bool operator ==(Stock x, Stock y)
    > {
    > return x.Equals(y);
    > }[/color]

    you need to check if x is not null

    if (x == null)
    return y == null;
    else
    return x.Equals(y);


    Comment

    • Christoph Nahr

      #3
      Re: Overloading operator== and comparing to null

      On Wed, 30 Jul 2003 15:03:35 -0400, "Jack Hanebach"
      <hanebach.no@sp am.suscom.net> wrote:
      [color=blue]
      >you need to check if x is not null
      >
      >if (x == null)
      > return y == null;
      >else
      > return x.Equals(y);[/color]

      Watch out for a nasty trap here. Your code will result in an infinite
      recursion because (x == null) invokes the strongly typed overload of
      operator== which is currently executing.

      You have to use a System.Object comparison instead, such as:

      if ((object) x == null)
      return ((object) y == null);
      else
      return x.Equals(y);
      --
      Stay ahead in World of Warcraft with expert guides, latest patch news, class tips, dungeon strategies, PvP builds, and The War Within updates—all in one place.

      Comment

      • Jack Hanebach

        #4
        Re: Overloading operator== and comparing to null

        "Christoph Nahr" <christoph.nahr @kynosarges.de> wrote in message
        news:dt5givo2mf 38l44215vfqtid7 q28nk62ai@4ax.c om...
        [color=blue][color=green]
        > >if (x == null)
        > > return y == null;
        > >else
        > > return x.Equals(y);[/color]
        >
        > Watch out for a nasty trap here. Your code will result in an infinite
        > recursion because (x == null) invokes the strongly typed overload of
        > operator== which is currently executing.
        >
        > You have to use a System.Object comparison instead, such as:
        >
        > if ((object) x == null)
        > return ((object) y == null);
        > else
        > return x.Equals(y);[/color]

        Good catch! I guess if (null == x) would work too...


        Comment

        • Jon Skeet

          #5
          Re: Overloading operator== and comparing to null

          Jack Hanebach <hanebach.no@sp am.suscom.net> wrote:[color=blue]
          > Good catch! I guess if (null == x) would work too...[/color]

          No, that would still call the overloaded == operator. See section
          14.2.4 of the C# specification - the order isn't taken into account
          when selecting the operator implementation to use.

          I'd also argue that even if it *did* work, it would be a bad way of
          doing it, as anyone just looking at it might not realise the purpose of
          specifying the operator that particular way, and change it to the more
          natural (in English) if (x==null). Fortunately, that's not an issue
          anyway :)

          --
          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

          Working...