繁体   English   中英

为什么分配此对象后该对象为null?

[英]Why is this object null after being assigned?

完整课程在这里: https : //gist.github.com/archer884/453c9937afa2dcb74f24

在我的班级(某种树)中,我有一个Node<T> _root;

在Add方法中,此变量被分配给一个名为简单node的局部变量,然后我们在node上做了一些工作,如下所示。

while (node != null)
{
    nodeDimensions = Dimensions(node.Item);
    if (dimensions.SequenceEqual(nodeDimensions))
        throw new ArgumentException("An element with the same key already exists in the tree.");

    dimension = depth % dimensions.Count;
    comparison = dimensions[dimension].CompareTo(nodeDimensions[dimension]);

    if (comparison <= 0)
    {
        node = node.LeftChild;
        continue;
    }
    else
    {
        node = node.RightChild;
        continue;
    }
}
node = new KdTreeNode<TItem>(item);

因此,对我来说,这似乎是我只是在从该变量中分配复活节彩蛋,但我的测试用例却不同意: Assert.NotNull(tree.Root); 总是失败。

编辑:对于将来的读者,我将注意以下内容。

if (_root == null)
{
    _root = new KdTreeNode<TItem>(item);
    return;
}

这样,当我们开始分配内容时,_root不会引用null。 我认为。 也许其他家伙之一会澄清。

在代码中的任何地方都没有实际分配给_root字段,因此tree.Root始终为null 您需要将_root分配给Add计算的值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM