Why is the LinkedListNode class immutable?

I am curious to know why all the properties (Next, Previous, Value, etc.) set by the LinkedListNode class are readonly ie, these properties do not have their respective setters?

I tried to solve a small list-related algorithmic problem and decided to use the .Net built-in to the LinkedList class. But due to the readonly behavior of the LinkedListNode properties, I cannot override the values ​​or change the Next pointer for any given node.

thank

+4
source share
2 answers

Valuehas setter, ad public T Value { get; set; }.

Next Previous , LinkedList .

LinkedList , , , .

, , AddBefore, , LinkedListNode , node.Next.Previous node, ..

+6

Next/Previous - , , . LinkedList<T> , , . , .

, MSDN, Value , LinkedListNode<T> , , !

// Note following class is not serializable since we customized the serialization of LinkedList. 
[System.Runtime.InteropServices.ComVisible(false)] 
public sealed class LinkedListNode<T> {
    internal LinkedList<T> list;
    internal LinkedListNode<T> next;
    internal LinkedListNode<T> prev;
    internal T item;

    public LinkedListNode( T value) {
        this.item = value;
    }

    internal LinkedListNode(LinkedList<T> list, T value) {
        this.list = list;
        this.item = value;
    }

    public LinkedList<T> List {
        get { return list;}
    }

    public LinkedListNode<T> Next {
        get { return next == null || next == list.head? null: next;}
    }

    public LinkedListNode<T> Previous {
        get { return prev == null || this == list.head? null: prev;}
    }

    public T Value {
        get { return item;}
        set { item = value;}
    }

    internal void Invalidate() {
        list = null;
        next = null;
        prev = null;
    }           
}  

, .

+2

All Articles