Computer Science Canada

Linked List Problem in C#

Author:  person [ Sat Nov 22, 2008 7:38 pm ]
Post subject:  Linked List Problem in C#

In this problem, foo and bar are both single linked lists. Next is a member of the list which points to the next element in the list.

code:

foo = bar;                             
Console.WriteLine (bar.Next==null);
foo.Next = null;
Console.WriteLine (bar.Next==null);


When I run this portion of the code in my program, the output is:

False
True

Why is this?

Author:  person [ Sat Nov 22, 2008 8:58 pm ]
Post subject:  Re: Linked List Problem in C#

Btw, the linked lists are instances of a class called List which contains a variable to hold a value and a variable to hold a variable of type List which points to another value and List. So I don't think this is an issue of pointers since I don't believe they are pointing to the same location in memory.

Author:  OneOffDriveByPoster [ Sat Nov 22, 2008 10:53 pm ]
Post subject:  Re: Linked List Problem in C#

person @ Sat Nov 22, 2008 7:38 pm wrote:
code:

foo = bar;
Why do you believe that foo would be different from bar after that?

Author:  rdrake [ Sat Nov 22, 2008 11:52 pm ]
Post subject:  RE:Linked List Problem in C#

Foo is not a clone of bar, it's just a reference to it. Try making the line say something like foo = bar.Clone() and see what happens.


: