获取 LinkedList<T> 的第一个元素。

4

我是一名初学者程序员,我在C#中遇到了这个问题。解决方案可能很简单,但这不由我决定。

我有一个自定义类,它继承了LinkedList,我需要一个方法来返回第一个元素并从列表中删除它。 代码:

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;
    }
}

问题是this.First返回了LinkedListNode。我尝试过以下方法:

LinkedListNode<CustomElement> first = this.First;

但是返回语句失败了,因为方法的类型是CustomElement


如果您想在读取第一个元素时将其删除(出队),请考虑使用队列。 - nunespascal
1
不知道为什么这个帖子会有这么多的踩。我通过谷歌很快就找到了它,并且里面包含了我需要解决问题的确切信息。 - fIwJlxSzApHEZIl
1个回答

11

文档所述,LinkedListNode<T>Value属性可用于访问存储在列表项中的值。因此,请将CustomElement ce = this.First.Value;赋值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接