Why can't I inherit LinkedListNode <T>?
In .NET 3.5, I use the LinkedList class, but I have the following problem. I want the items in this list to keep up with the previous and next items in the list. In other words, I want the method in the elements to be able to do this. Next, this.Previous. Is it 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!
Firstly, LinkedListNode sealed so it cannot be inherited.
Secondly, LinkedListNode contains the Previous and Next properties that relate to the previous and next nodes in LinkedList , from which this LinkedListNode instance came.
Finally, in order to use AddLast , 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 are wrong.
The .AddLast(T) method returns a linked list node. This indicates your day and has the previous and next features that you are looking for.
According to MSDN , the LinkedListNode class cannot be inherited.
Michael points out that this blog post is from Eric Lippert about why many classes in the structure are sealed
Day d1 = new Day(); Day d2 = new Day(); LinkedList<Day> days = new LinkedList<Day>(); // Day instance doesn't have Next. Its the LinkedListNode that should be used. LinkedListNode<Day> d1Node = days.AddLast(d1); days.AddLast(d2); Why not just use the nodes directly to start with ...
LinkedListNode<Day> d1 = new LinkedListNode<Day>(new Day()); LinkedListNode<Day> d2 = new LinkedListNode<Day>(new Day()); LinkedList<Day> days = new LinkedList<Day>(); days.AddLast(d1); days.AddLast(d2); // Now you can read the node directly d1.Next... // If you need to place it somewhere other than at the end (like say you want d2 before d1, // but d1 is already in the list) use 'AddBefore' and 'AddAfter' eg: days.AddLast(d2); days.AddBefore(d2, d1); The only way I can think that common T knows his siblings is to implement their own data structure.
public interface ILinkedListNode { ILinkedListNode Next { get; set; } ILinkedListNode Previous { get; set; } } public class LinkedList<T> where T : ILinkedListNode { /*.... methods here ...*/ } public class Node : ILinkedListNode { public Node Next { get; set; } public Node Previous { get; set; } ILinkedListNode ILinkedListNode.Next { get { return this.Next; } set { this.Next = (Node)value; } } ILinkedListNode ILinkedListNode.Previous { get { return this.Previous; } set { this.Previous = (Node)value; } } } LinkedListNode is a private class, so it cannot be inherited. We are so corrupted by IEnumerable that we lose sight of the actual data structures that we use. It is still a linked list, so you can treat it as one:
LinkedListNode<Day> node = days.First(); while (node != null) { Day day = node.Value; // do stuff here... node = node.Next(); }