gpt4 book ai didi

c# - 包装 List.Add() 给出 - 未将对象引用设置为对象的实例

转载 作者:太空狗 更新时间:2023-10-29 21:13:35 26 4
gpt4 key购买 nike

我想用我的自定义类包装 List 类。至于现在我有这样的东西;

public class PriorityListOfNodes
{

private List<Node> list_;
private IComparer<Node> sorter_;

public List<Node> List_ {
get {return list_;}
set {list_ = value;}
}

public PriorityListOfNodes ()
{
sorter_ = new NodeSorter_fValueDescending ();
}

public Node PopFromEnd ()
{
Node temp = new Node (list_ [list_.Count - 1]);
list_.RemoveAt (list_.Count - 1);
return temp;
}

public Node PeekFromEnd ()
{
return list_ [list_.Count - 1];
}

public void Add (ref Node toAdd)
{
Debug.Log (toAdd);
list_.Add (toAdd);
list_.Sort (sorter_);
}
}

当我现在做

Node temp = new Node(10,20); //custom constructor
PriorityListOfNodes l = new PriorityListOfNodes();
l.add(temp);

我得到运行时异常:

Object reference not set to an instance of an object

我也尝试过不使用 ref 但结果相同。我在这里做错了什么?

最佳答案

您实际上从未实例化 List<Node> .

 public PriorityListOfNodes ()
{
sorter_ = new NodeSorter_fValueDescending ();
list_ = new List<Node>();
}

关于c# - 包装 List.Add() 给出 - 未将对象引用设置为对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13982358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com