user1189571
user1189571

Reputation: 305

Get first element of LinkedList<T>

I'm a beginner programmer and I have this problem in C#. The solution is probably easy, but that's not for me to decide.

I have this custom class that inherits LinkedList and I need a method to return first element and remove it from list. Code:

class CustomClass : LinkedList<CustomElement>
{
    public CustomElement getFirstElement(){
        //here is the problem and I don't know how to solve it
        CustomElement ce = this.First;
        this.RemoveFirst();
        return first;
    }
}

Problem is that this.First returns LinkedListNode. I tried this:

LinkedListNode<CustomElement> first = this.First;

But then the return statement fails, because type of method is CustomElement.

Upvotes: 6

Views: 10786

Answers (1)

O. R. Mapper
O. R. Mapper

Reputation: 20780

As described in the documentation, the Value property of LinkedListNode<T> can be used to access the value stored in the list item. Therefore, assign CustomElement ce = this.First.Value;.

Upvotes: 12

Related Questions