首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么有时两个对象引用相同但不总是相同

为什么有时两个对象引用相同但不总是相同
EN

Stack Overflow用户
提问于 2019-02-18 20:53:49
回答 1查看 91关注 0票数 2

下面是最后一个答案:Recursive method to convert flat collection to hierarchal collection?

我想对层次结构之外的另一个对象使用相同的方法CreateTree : ItemNode:

代码语言:javascript
运行
复制
public class ItemNode
{
    public string Id { get; set; }
    public Item Item { get; set; }
    public ICollection<ItemNode> Children { get; set; }
}

以及项目的定义:

代码语言:javascript
运行
复制
public class Item
{
    public string ID { get; set; }
    public string Name { get; set; }
    public int Level { get; set; }
    public string ParentId { get; set; }
}

下面是带有ItemNode的CreateTree方法:

代码语言:javascript
运行
复制
    static List<ItemNode> CreateTreeItems(IEnumerable<ItemNode> nodes)
    {
        Dictionary<string,ItemNode> idToNode = nodes.ToDictionary(n => n.Id, n => n);
        List<ItemNode> roots = new List<ItemNode>();
        ItemNode root = null;

        foreach (var n in nodes)
        {
            if (n.Item.ParentId == null)
            {
                if (root != null)
                {
                    roots.Add(root);
                }
                root = n;
                continue;
            }

            ItemNode parent = idToNode[n.Item.ParentId];
            //if (!idToNode.TryGetValue(n.Item.ParentId, out parent))
            //{
            //  //Parent doesn't exist, orphaned entry
            //}

            parent?.Children.Add(n);

            // RETURNS FALSE WHEREAS IN THE ORIGINAL METHOD IT RETURNS TRUE
            var test = Object.ReferenceEquals(parent, root);
            Debug.WriteLine(test);
        }


        if (root == null)
        {
            //There was no root element
        }

        roots.Add(root);
        return roots;
    }

它不起作用,因为父对象和根对象不引用相同的对象(而在原始方法中,它引用了)。我猜这是因为我向ItemNode类添加了一个Item属性。但我不知道该怎么解决它。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-18 21:54:43

为什么您期望根节点和父节点引用相等?每个根节点都可能是父节点,但并非每个父节点都是根节点。

可能会出现引用相等的情况,但这在很大程度上取决于nodes集合的排序顺序。实际上,当根节点放置在第一级子节点之前时,您将看到引用相等的情况。

我认为您的问题出在其他地方,例如,如果Nodes集合根本没有根节点,它可能无法工作。

这是一个例子,在这里试试https://dotnetfiddle.net/4r52xP

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;

public class Hierarchy
{
    public Hierarchy(string iD, string name, int level, string parentID, string topParent)
    {
        ID = iD;
        Name = name;
        Level = level;
        ParentID = parentID;
        Children = new HashSet<Hierarchy>();
    }
    public string ID { get; set; }
    public string Name{ get; set; }
    public int Level { get; set; }
    public string ParentID { get; set; }
    public ICollection<Hierarchy> Children { get; set; }
}

public class Program
{
    static Hierarchy CreateTree(IEnumerable<Hierarchy> Nodes)
    {
        var idToNode = Nodes.ToDictionary(n => n.ID, n => n);

        Hierarchy root = null;
        foreach (var n in Nodes)
        {
            if (n.ParentID == null)
            {
                if (root != null)
                {
                    //there are multiple roots in the data
                }
                root = n;
                continue;
            }

            Hierarchy parent = null;
            if (!idToNode.TryGetValue(n.ParentID, out parent))
            {
                //Parent doesn't exist, orphaned entry
            }

            parent.Children.Add(n);

            Console.WriteLine("ReferenceEquals: {0}", Object.ReferenceEquals(parent, root));
        }

        if (root == null)
        {
            //There was no root element
        }
        return root;
    }

    public static void Main()
    {
        Console.WriteLine("Test #1");

        List<Hierarchy> l = new List<Hierarchy>();
        l.Add(new Hierarchy("295152","name1",1,null, null)); // <-root node at the top of the list
        l.Add(new Hierarchy("12345","child1",2,"295152", null));
        l.Add(new Hierarchy("54321","child2",2,"295152", null));
        l.Add(new Hierarchy("44444","child1a",3,"12345", null));
        l.Add(new Hierarchy("33333","child1b",3,"12345", null));
        l.Add(new Hierarchy("22222","child2a",3,"54321", null));
        l.Add(new Hierarchy("22221","child2b",3,"54321", null));
        l.Add(new Hierarchy("22002","child2c",3,"54321", null));
        l.Add(new Hierarchy("20001","child2a2",4,"22222", null));
        l.Add(new Hierarchy("20101","child2b2",4,"22222", null));       

        CreateTree(l);

        Console.WriteLine("\nTest #2");

        l = new List<Hierarchy>();
        l.Add(new Hierarchy("12345","child1",2,"295152", null));
        l.Add(new Hierarchy("54321","child2",2,"295152", null));
        l.Add(new Hierarchy("44444","child1a",3,"12345", null));
        l.Add(new Hierarchy("33333","child1b",3,"12345", null));
        l.Add(new Hierarchy("22222","child2a",3,"54321", null));
        l.Add(new Hierarchy("22221","child2b",3,"54321", null));
        l.Add(new Hierarchy("22002","child2c",3,"54321", null));
        l.Add(new Hierarchy("20001","child2a2",4,"22222", null));
        l.Add(new Hierarchy("20101","child2b2",4,"22222", null));
        l.Add(new Hierarchy("295152","name1",1,null, null)); // <-root node at the bottom of the list
        CreateTree(l);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54747804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档