简体   繁体   English

为什么当两个对象相同时我的C#代码找不到?

[英]Why can't my C# code find out when two objects are identical?

This is a method for recursively searching through a binary tree that is filled with objects of the various child classes of the class Photo. 这是一种用于递归搜索二叉树的方法,该二叉树充满了Photo类的各个子类的对象。

public void SearchForIdentical(Photo searched)
{
    SearchForIdentical(ref root, searched);
}

void SearchForIdentical(ref TreeNode current, Photo searched)
{
    try
    {
        if(current != null)
        {
            if(current.content.Equals(searched))
            {
                throw new PhotoAlreadyExistsException(searched);
            }
            SearchForIdentical(ref current.left, searched);
            SearchForIdentical(ref current.right, searched);
        }
    }
    catch (PhotoAlreadyExistsException e)
    {
        Console.WriteLine("This photo already exists! Try a new one!");
    }
}

A 'photo' is defined by the value of its various arguments and properties (the child classes don't necessarily all have the same kind of arguments and properties). “照片”由其各种参数和属性的值定义(子类不一定都具有相同类型的参数和属性)。 If the user types in a new photo, that is fully identical in all its arguments and properties with an existing one, this method should notice it, and throw an exception. 如果用户键入一张新照片,其所有参数和属性都与现有照片完全相同,则此方法应予以注意,并引发异常。

The problem is, it never happens. 问题是,它永远不会发生。 When I debugged it, it seemed the program never assigns true to the premise current.content.Equals(searched) , even when the two object is exactly identical. 当我调试它时,即使两个对象完全相同,该程序似乎也从未将true赋给前提current.content.Equals(searched)

What could be the problem? 可能是什么问题呢?

If you never override the Equals() method in the Photo class to do the comparisons of the properties you've described, your code will be doing an object reference equality comparison. 如果您从不重写Photo类中的Equals()方法来对您描述的属性进行比较,则您的代码将进行对象引用相等性比较。

This would explain why when the properties are identical, the Equals() method returns false . 这可以解释为什么当属性相同时, Equals()方法返回false

暂无
暂无

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

相关问题 为什么单声道C#找不到我的代码System.IO.File? - Why can't mono C# find System.IO.File with my code? 初学者尝试在 C# 中制作天气预报应用程序,无法弄清楚为什么我的代码不起作用 - Beginner trying to make a weather forecast app in C#, can't figure out why my code won't work 为什么我的 C# 在 SharePoint 上找不到任何列表? - Why can't my C# find any lists on SharePoint? C#为什么在工具箱中找不到DataGridView? - C# Why can't I find DataGridView in my toolbox? 在 C# Generics 中这两行代码是否相同? - In C# Generics are these two lines of code identical? C#代码在两个字符串之间找到一个字符串-相同的相同字符串 - C# code to find a string between two strings - same identical strings 似乎找不到从表列中取出值并将其放在标签C#上的方法 - Can't seem to find how to take a value out of my table column and put it on a label C# 我有一个对象列表,但是找不到整理代码的方法。 我正在尝试在C#中的特定索引处更新对象 - I have a list of objects but I can't find a way to tidy up my code. I'm trying to update an object at a specific index in C# C#输出参数,为什么我给我的变量赋值,仍然无法返回? - c# out parameter, why I have assigned the value to my variable, still can't return? 两个相同的字符串可以是C#中的两个独立实例吗? - Can two identical strings be two separate instances in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM