CSharp · Console
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
var tests1 = new[] { new TestWithOverride(1), new TestWithOverride(2), new TestWithOverride(3) };
var contains = tests1.Contains(new TestWithOverride(2));
Console.WriteLine(contains);
var tests2 = new[] { new TestWithoutOverride(1), new TestWithoutOverride(2), new TestWithoutOverride(3) };
contains = tests2.Contains(new TestWithoutOverride(2));
Console.WriteLine(contains);
}
}
internal class TestWithOverride : IEquatable<TestWithOverride>
{
public TestWithOverride(int id)
{
Id = id;
}
public int Id { get;}
public bool Equals(TestWithOverride other) => this.Id == other.Id;
public override bool Equals(object obj)
{
var test = obj as TestWithOverride;
return test == null ? obj.Equals(this) : Equals(test);
}
}
internal class TestWithoutOverride : IEquatable<TestWithoutOverride>
{
public TestWithoutOverride(int id)
{
Id = id;
}
public int Id { get;}
public bool Equals(TestWithoutOverride other) => this.Id == other.Id;
}