【问题标题】:comparing two objects of same type return false in c#在c#中比较两个相同类型的对象返回false
【发布时间】:2020-02-11 15:39:56
【问题描述】:

我有两个类列表 lin

public class lin
        {
            public string DB_Name;
            public string Object_Name;
        }
List<lin> lines1 = new List<lin>();
List<lin> lines2 = new List<lin>();

我已经为这两个列表分配了一些值 下面是列表 lines1 的输出,位于即时窗口的索引 5,其中包含 DB_Name = "aesdb_s1"Object_Name = "tblAsiAliasItem"

 lines1[5] 
        DB_Name: "aesdb_s1"
        Object_Name: "tblAsiAliasItem"

索引 0(零)处的 lines2 也具有相同的值

lines2[0]
    DB_Name: "aesdb_s1"
    Object_Name: "tblAsiAliasItem"

但是当我比较这两个对象或尝试获取值索引时,它返回 false

lines1.IndexOf(lines2[0])
-1

lines1.Contains(lines2[0]);
false

lines1[5]==lines2[0]
false

以上是visual studio的即时窗口输出

【问题讨论】:

  • 您是如何比较这些列表的?请也发布该代码
  • 如何将对象添加到列表中?
  • 我已经发布了即时窗口的输出,您可以在我的问题中看到
  • 它不会通过== 来复制它们。你应该覆盖equals
  • 默认情况下,类提供基于引用的相等性。我假设您的对象是语义等价的,但是是不同的对象实例。这还不够:它们会报告不同。

标签: c# list


【解决方案1】:

这是一个lin,它可以像你期望的那样工作:

public class Lin : IEquatable<Lin>
{
    public string DbName {get;set;}
    public string ObjectName {get;set;}

    public override bool Equals(object obj) {
        return Equals(obj as Lin);
    }
    public bool Equals(Lin other) {
        return other != null
           && this.DbName == other.DbName
           && this.ObjectName == other.ObjectName;
    }
    public override int GetHashCode() {
        int x = DbName == null ? 0 : DbName.GetHashCode();
        int y = ObjectName == null ? 0 : ObjectName.GetHashCode();
        return (-1423 * x) ^ y;
    }
}

【讨论】:

  • 请注意,这里假定DbNameObjectName 区分大小写。
  • @Brian 是的;这是一个相当不错的假设;不区分大小写应该是明确的......
【解决方案2】:

您正在通过引用比较对象。不是按价值...

https://msdn.microsoft.com/en-us/library/dd183752.aspx

如果你想比较它们,你需要在你的lin类中使用override the Equals()方法来具体比较每个属性。

【讨论】:

    【解决方案3】:

    即使对于==,这也将为您提供完整的解决方案:

     public class lin : IEquatable<lin>
            {
                public string DB_Name;
                public string Object_Name;
    
                public bool Equals(lin other)
                {
                    if (ReferenceEquals(null, other)) return false;
                    if (ReferenceEquals(this, other)) return true;
                    return string.Equals(DB_Name, other.DB_Name) && string.Equals(Object_Name, other.Object_Name);
                }
    
                public override bool Equals(object obj)
                {
                    if (ReferenceEquals(null, obj)) return false;
                    if (ReferenceEquals(this, obj)) return true;
                    if (obj.GetType() != this.GetType()) return false;
                    return Equals((lin) obj);
                }
    
                public override int GetHashCode()
                {
                    unchecked { return ((DB_Name != null ? DB_Name.GetHashCode() : 0)*397) ^ (Object_Name != null ? Object_Name.GetHashCode() : 0); }
                }
    
                public static bool operator ==(lin left, lin right)
                {
                    return Equals(left, right);
                }
    
                public static bool operator !=(lin left, lin right)
                {
                    return !Equals(left, right);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 2019-05-29
      相关资源
      最近更新 更多