How many nulls are there?

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

    How many nulls are there?

    I was browsing the MSDN class library for the Object class, and tested
    its sample code.

    If we have

    Object o = null;
    Object p = null;

    Then

    Object.Referenc eEquals(o, p);

    will give me true. This seems to mean that o and p are both pointing
    to the same object. Fair enough.

    But, if we instantiate another Object q

    Object q = new Object();
    q = null;

    Now,

    Object.Referenc eEquals(o, q);

    gives us false, although both o and q have a null value. This seems
    to mean that o and q are pointing to different stuffs. The question
    is, why when we do:

    Object o = null;
    Object p = null;

    o and q seem to point to the same stuff?

    I am not sure if I made my question clear.
  • Jeroen Mostert

    #2
    Re: How many nulls are there?

    Author wrote:
    I was browsing the MSDN class library for the Object class, and tested
    its sample code.
    >
    If we have
    >
    Object o = null;
    Object p = null;
    >
    Then
    >
    Object.Referenc eEquals(o, p);
    >
    will give me true. This seems to mean that o and p are both pointing
    to the same object. Fair enough.
    >
    No. Neither o nor p are pointing to any object. That's the point of null: to
    allow a reference to refer to no object. The references have the same value
    here, that much is true.
    But, if we instantiate another Object q
    >
    Object q = new Object();
    q = null;
    >
    Now,
    >
    Object.Referenc eEquals(o, q);
    >
    gives us false, although both o and q have a null value.
    Really? Try giving a complete, working program that exhibits this behavior.
    I couldn't.

    --
    J.

    Comment

    • Author

      #3
      Re: How many nulls are there?

      On Jul 2, 4:50 pm, Jeroen Mostert <jmost...@xs4al l.nlwrote:
      Author wrote:
      I was browsing the MSDN class library for the Object class, and tested
      its sample code.
      >
      If we have
      >
      Object o = null;
      Object p = null;
      >
      Then
      >
      Object.Referenc eEquals(o, p);
      >
      will give me true.  This seems to mean that o and p are both pointing
      to the same object. Fair enough.
      >
      No. Neither o nor p are pointing to any object. That's the point of null:to
      allow a reference to refer to no object. The references have the same value
      here, that much is true.
      >
      But, if we instantiate another Object q
      >
      Object q = new Object();
      q = null;
      >
      Now,
      >
      Object.Referenc eEquals(o, q);
      >
      gives us false, although both o and q have a null value.
      >
      Really? Try giving a complete, working program that exhibits this behavior.
      I couldn't.
      >
      --
      J.

      Try this and you'll see:

      using System;

      namespace ConsoleApplicat ion1
      {
      class ObjectEqualsTes t
      {
      public static void Main()
      {
      object o = null;
      object p = null;
      object q = new Object();

      Console.WriteLi ne("o=null, p=null -- " +
      Object.Referenc eEquals(o, p));
      Console.WriteLi ne("p=null; q=new Object() --" +
      Object.Referenc eEquals(p, q));

      p = q;
      Console.WriteLi ne("p=q --" + Object.Referenc eEquals(p,
      q));

      q = null;
      Console.WriteLi ne("q = null; o=null; o and q --" +
      Object.Referenc eEquals(o, p));

      Console.Read();
      }
      }
      }

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: How many nulls are there?

        Author <gnewsgroup@gma il.comwrote:
        I was browsing the MSDN class library for the Object class, and tested
        its sample code.
        >
        If we have
        >
        Object o = null;
        Object p = null;
        >
        Then
        >
        Object.Referenc eEquals(o, p);
        >
        will give me true. This seems to mean that o and p are both pointing
        to the same object. Fair enough.
        As Jeroen said, neither of them are pointing to any object.
        But, if we instantiate another Object q
        >
        Object q = new Object();
        q = null;
        >
        Now,
        >
        Object.Referenc eEquals(o, q);
        >
        gives us false, although both o and q have a null value.
        That's not what your code tests. Look at this statement carefully:

        Console.WriteLi ne("q = null; o=null; o and q --" +
        Object.Referenc eEquals(o, p));

        Look at what variables it's *claiming* to compare, then look at the
        actual method arguments...

        --
        Jon Skeet - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon_skeet
        C# in Depth: http://csharpindepth.com

        Comment

        • Ben Voigt [C++ MVP]

          #5
          Re: How many nulls are there?

          >Now,
          >>
          >Object.Referen ceEquals(o, q);
          >>
          >gives us false, although both o and q have a null value.
          >
          That's not what your code tests. Look at this statement carefully:
          >
          Console.WriteLi ne("q = null; o=null; o and q --" +
          Object.Referenc eEquals(o, p));
          >
          Look at what variables it's *claiming* to compare, then look at the
          actual method arguments...
          Although to be fair, q and p look very much alike with some fonts.


          Comment

          • Author

            #6
            Re: How many nulls are there?

            On Jul 2, 5:10 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
            Author <gnewsgr...@gma il.comwrote:
            I was browsing the MSDN class library for the Object class, and tested
            its sample code.
            >
            If we have
            >
            Object o = null;
            Object p = null;
            >
            Then
            >
            Object.Referenc eEquals(o, p);
            >
            will give me true. This seems to mean that o and p are both pointing
            to the same object. Fair enough.
            >
            As Jeroen said, neither of them are pointing to any object.
            >
            But, if we instantiate another Object q
            >
            Object q = new Object();
            q = null;
            >
            Now,
            >
            Object.Referenc eEquals(o, q);
            >
            gives us false, although both o and q have a null value.
            >
            That's not what your code tests. Look at this statement carefully:
            >
            Console.WriteLi ne("q = null; o=null; o and q --" +
            Object.Referenc eEquals(o, p));
            >
            Look at what variables it's *claiming* to compare, then look at the
            actual method arguments...
            >
            // blush for being fooled by the typos of mine.

            This is probably one reason we should have more friendly variable
            names such as variable1, number1, number2.

            Comment

            • Author

              #7
              Re: How many nulls are there?

              On Jul 2, 6:12 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
              Now,
              >
              Object.Referenc eEquals(o, q);
              >
              gives us false, although both o and q have a null value.
              >
              That's not what your code tests. Look at this statement carefully:
              >
              Console.WriteLi ne("q = null; o=null; o and q --" +
              Object.Referenc eEquals(o, p));
              >
              Look at what variables it's *claiming* to compare, then look at the
              actual method arguments...
              >
              Although to be fair, q and p look very much alike with some fonts.
              Especially when one quite often sees the left as if right and sees the
              right as if left, such as me. :-) I need to *see* my p's and q's.

              Comment

              Working...