Zulqarnain Jalil
Zulqarnain Jalil

Reputation: 1691

comparing two objects of same type return false in c#

I have Two lists of Class lin

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

I have assign some values to these two lists below is the output of list lines1 at index 5 from Immediate window which contains DB_Name = "aesdb_s1" and Object_Name = "tblAsiAliasItem"

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

also the lines2 at index 0 (zeero) have the same values

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

but when i compare these two objects or try to get index of value it returns false

lines1.IndexOf(lines2[0])
-1

lines1.Contains(lines2[0]);
false

lines1[5]==lines2[0]
false

Above is the output of immediate window from visual studio

Upvotes: 1

Views: 297

Answers (3)

Marc Gravell
Marc Gravell

Reputation: 1062865

Here's a lin that would work like you expect:

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;
    }
}

Upvotes: 3

Royi Namir
Royi Namir

Reputation: 148524

This will give you the whole solution even for == :

 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);
            }
        }

Upvotes: 0

Wyatt Earp
Wyatt Earp

Reputation: 1823

You are comparing the objects by reference. Not by value...

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

If you want to compare them, you need to override the Equals() method in your lin class to specifically compare each of the properties.

Upvotes: 2

Related Questions