
-----------------------------------
person
Sat Nov 22, 2008 7:38 pm

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.


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?

-----------------------------------
person
Sat Nov 22, 2008 8:58 pm

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.

-----------------------------------
OneOffDriveByPoster
Sat Nov 22, 2008 10:53 pm

Re: Linked List Problem in C#
-----------------------------------

foo = bar;
Why do you believe that foo would be different from bar after that?

-----------------------------------
rdrake
Sat Nov 22, 2008 11:52 pm

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.
