-1

I tried to use GroupBy not with anonymous type but with concrete class as a key. I overrided Equals method. It didn't help. I saw the issues about using concrete types as a key, but there wasn't said about any troubles using it. The debug session results are in attached imagepicture

class Program
{
    static void Main(string[] args)
    {
        DateTime val = DateTime.Now;
        bool testEquality = new Class2(val) == new Class2(val);
        bool testEquality2 = new Class2(val).Equals(new Class2(val));
        bool testEquality3 = new Class2(val).Equals(new Class2(val.AddDays(1)));
        List<Class1> list = new List<Class1>
        {
            new Class1(1, val),
            new Class1(2, val.AddHours(1)),
            new Class1(2, val.AddHours(2)),
            new Class1(2, val.AddHours(1)),
            new Class1(2, val.AddHours(1)),
        };
        var grouped = list.GroupBy(x => new Class2(x.prop2));

    }
}
internal class Class1
{
    public int prop1;
    public DateTime prop2;
    public Class1(int v1, DateTime v2)
    {
        prop1 = v1;
        prop2 = v2;
    }
}
internal class Class2
{
    public DateTime prop2;
    public Class2(DateTime v)
    {
        prop2 = v;
    }
    public override bool Equals(object obj)
    {
        return this.prop2 == ((Class2)obj).prop2;
    }
}
Evg
  • 98
  • 2
  • 7
  • 3
    What is exactly the problem? What is the error you're facing? – Paul Karam Jul 20 '17 at 07:52
  • @Evg What value are you expecting for `testEquality`? Also, what would you expect the result of `new Class2(val).Equals("")` to be? Or `new Class2(val).Equals(null)`? – mjwills Jul 20 '17 at 09:11

2 Answers2

1

You need to override GetHashCode in Class2 too.

internal class Class2
{
    public DateTime prop2;
    public Class2(DateTime v)
    {
        prop2 = v;
    }
    public override bool Equals(object obj)
    {
        return this.prop2 == ((Class2)obj).prop2;
    }

    public override int GetHashCode()
    {
        return this.prop2.GetHashCode();
    }
}

Debugger

Best practice when overriding Equals:

Why is it important to override GetHashCode when Equals method is overridden?

Tom John
  • 783
  • 5
  • 14
  • I tested the sample code as posted and the grouping changed from 5 distinct to 3 (see screenshot). I assume this is the problem the OP is trying to overcome? – Tom John Jul 20 '17 at 08:41
  • With your code what was the value of `testEquality`? – mjwills Jul 20 '17 at 08:48
1

You need to implement the == and != operators, all of the Equals methods and GetHashCode to get 'proper' equality checking in C#. Below is an example.

public class Class2
{
    public DateTime prop2;
    public Class2(DateTime v)
    {
        prop2 = v;
    }

    public static bool operator ==(Class2 a, Class2 b)
    {
        if (ReferenceEquals(a, b))
        {
            return true;
        }

        return (object)a != null && a.Equals(b);
    }

    public static bool operator !=(Class2 a, Class2 b)
    {
        return !(a == b);
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as Class2);
    }

    protected bool Equals(Class2 other)
    {
        return other != null && prop2.Equals(other.prop2);
    }

    public override int GetHashCode()
    {
        return prop2.GetHashCode();
    }
}
mjwills
  • 23,389
  • 6
  • 40
  • 63