微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – Wrapping List.Add()给出 – 对象引用未设置为对象的实例

我想用自定义类包装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

我也试过没有参考,但结果相同.我在这做错了什么?

解决方法

您永远不会实际实例化List< Node>.
public PrioritylistofNodes ()
    {
            sorter_ = new NodeSorter_fValueDescending ();
            list_ = new List<Node>();
    }

原文地址:https://www.jb51.cc/csharp/92047.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐