In .NET 3.5, I am using the LinkedList class but I am having the following issue. I want the items of that list to be aware of the previous and next items in the list. In other words, I want the method in the items to be able to do this.Next, this.Previous. Is this possible? Below is an example of what I would like to do.
Day d1 = new Day();
Day d2 = new Day();
LinkedList<Day> days = new LinkedList<Day>();
days.AddLast(d1);
days.AddLast(d2);
// Here is want I would like to do
d1.Next = ...
Thanks!
First, LinkedListNode
is sealed
so it can not be inherited.
Secondly, LinkedListNode
does contain properties Previous
and Next
that refer to the previous and next nodes in the LinkedList
that a given instance of LinkedListNode
came from.
Lastly, to use AddLast
correctly, you must do the following:
Day d1 = new Day();
Day d2 = new Day();
LinkedList<Day> days = new LinkedList<Day>();
LinkedListNode<Day> node1 = days.AddLast(d1);
LinkedListNode<Day> node2 = days.AddLast(d2);
// now node1.Next refers to node containing d2
// and node2.Previous referes to node containing d1
You're using it wrong.
The .AddLast(T)
method returns a linked list node. This points to your day and has the prev and next functions you are looking for.
According to the MSDN, the LinkedListNode class cannot be inherited.
Michael points out that this blog post from Eric Lippert talks about why many classes in the framework are sealed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With