Referenced Object

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

    Referenced Object

    Hello,

    My understanding about objects is that they are reference types and
    not value types but a simple code seems to defy my whole
    understanding.

    I wrote a simple console app:

    class Program
    {
    static void Main(string[] args)
    {
    A a1 = new A();
    a1.i = 1;
    a1.j = 2;

    A a2 = a1;

    a1 = null;

    Console.WriteLi ne(a2.i);
    }
    }

    class A
    {
    public int i;
    public int j;
    }

    My understanding is a2 and a1 should both both to the same location in
    the heap. Once a1 is made null, a2 should also become null but it does
    not happen?

    Why? Thats the question.
  • Peter Duniho

    #2
    Re: Referenced Object

    On Tue, 12 Aug 2008 08:40:18 -0700, sandy <sandyiit@gmail .comwrote:
    [...]
    My understanding is a2 and a1 should both both to the same location in
    the heap. Once a1 is made null, a2 should also become null but it does
    not happen?
    Because "a1" and "a2" are variables, not the objects themselves. The
    variables _reference_ the objects; changes to the variables change only
    what they reference. Those changes can't affect other variables.

    Jon Skeet has an article that's not really about value vs. reference types
    per se, but I feel it does a pretty good job of describing this issue
    anyway:


    You may find that helpful in elaborating on the question.

    Pete

    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: Referenced Object

      On Aug 12, 11:40 am, sandy <sandy...@gmail .comwrote:
      Hello,
      >
      My understanding about objects is that they are reference types and
      not value types but a simple code seems to defy my whole
      understanding.
      >
      I wrote a simple console app:
      >
      class Program
          {
              static void Main(string[] args)
              {
                  A a1 = new A();
                  a1.i = 1;
                  a1.j = 2;
      >
                  A a2 = a1;
      >
                  a1 = null;
      >
                  Console.WriteLi ne(a2.i);
              }
          }
      >
          class A
          {
              public int i;
              public int j;
          }
      >
      My understanding is a2 and a1 should both both to the same location in
      the heap. Once a1 is made null, a2 should also become null but it does
      not happen?
      >
      Why? Thats the question.
      a2 and a1 are simply holding a reference to the object (think of it
      like they are holding the address of the object) , when you set a1 to
      null they simply makes references to different objetcs (or different
      addresses)

      As a matter of fact the "real" object will be kept in memory while at
      least one variable makes reference of him. After the last variable
      does not reference it, the object's memory can be collected.

      Comment

      • Jeff Louie

        #4
        Re: Referenced Object

        >>A a2 = a1;<<

        There is no call to a copy constructor here in C#. The reference
        variable a2 is
        assigned a reference to an existing object at this point. No new object
        is created
        by this call.

        At this point the variables a2 and a1 both hold references to a single
        instance of
        Class A.
        >>a1 = null;<<
        At this point the variable a1 no longer holds a reference to the single
        instance of
        Class A.
        But the variable a2 still holds a valid reference to the instance of
        Class A.

        Hope that helps,
        Jeff

        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

          #5
          Re: Referenced Object

          sandy wrote:
          Hello,
          >
          My understanding about objects is that they are reference types and
          not value types but a simple code seems to defy my whole
          understanding.
          >
          I wrote a simple console app:
          >
          class Program
          {
          static void Main(string[] args)
          {
          A a1 = new A();
          a1.i = 1;
          a1.j = 2;
          >
          A a2 = a1;
          >
          a1 = null;
          >
          Console.WriteLi ne(a2.i);
          }
          }
          >
          class A
          {
          public int i;
          public int j;
          }
          >
          My understanding is a2 and a1 should both both to the same location in
          the heap. Once a1 is made null, a2 should also become null but it does
          not happen?
          >
          Why? Thats the question.
          Because you are not setting the object to null, you are setting the
          reference to null.

          The a1 and a2 variables are references pointing to the same object.
          Changing the object through either reference changes the same object,
          but assigning a different reference (in this case null) to one variable
          neither changes the object nor the other variable.

          --
          Göran Andersson
          _____
          Göran Anderssons privata hemsida.

          Comment

          • sandy

            #6
            Re: Referenced Object

            On Aug 13, 5:57 am, Göran Andersson <gu...@guffa.co mwrote:
            sandy wrote:
            Hello,
            >
            My understanding about objects is that they are reference types and
            not value types but a simple code seems to defy my whole
            understanding.
            >
            I wrote a simple console app:
            >
            class Program
                {
                    static void Main(string[] args)
                    {
                        A a1 = new A();
                        a1.i = 1;
                        a1.j = 2;
            >
                        A a2 = a1;
            >
                        a1 = null;
            >
                        Console.WriteLi ne(a2.i);
                    }
                }
            >
                class A
                {
                    public int i;
                    public int j;
                }
            >
            My understanding is a2 and a1 should both both to the same location in
            the heap. Once a1 is made null, a2 should also become null but it does
            not happen?
            >
            Why? Thats the question.
            >
            Because you are not setting the object to null, you are setting the
            reference to null.
            >
            The a1 and a2 variables are references pointing to the same object.
            Changing the object through either reference changes the same object,
            but assigning a different reference (in this case null) to one variable
            neither changes the object nor the other variable.
            >
            --
            Göran Andersson
            _____http://www.guffa.com
            Thanks for the clarification. I think I get it now. I still have the C
            concepts in my mind while coding for C#.

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Referenced Object

              On Aug 13, 1:30 pm, sandy <sandy...@gmail .comwrote:
              Thanks for the clarification. I think I get it now. I still have the C
              concepts in my mind while coding for C#.
              When you've still got a C mindset, think that roughly "C pointer" ~=
              "C# reference" except you can't do pointer arithmentic.

              Jon

              Comment

              Working...