37
1
using System;
2
using System.Linq;
3
using System.Collections.Generic;
4
5
public class Program
6
{
7
public static void Main()
8
{
9
Person[] people = {
10
new Person(){ FirstName="Steve", LastName="Jobs"},
11
new Person(){ FirstName="Bill", LastName="Gates"},
12
new Person(){ FirstName="Steve", LastName="Jobs"},
13
new Person(){ FirstName="Lary", LastName="Page"}
14
};
15
16
var dist = people.Distinct().ToArray();
17
18
Array.ForEach(dist, v => Console.WriteLine(v.FirstName + " " + v.LastName));
19
}
20
}
21
22
class Person : IEquatable<Person>
23
{
24
public int Id { get; set; }
25
public string FirstName { get; set; }
26
public string LastName { get; set; }
27
28
public bool Equals(Person other)
29
{
30
return FirstName.Equals(other.FirstName) && LastName.Equals(other.LastName);
31
}
32
33
public override int GetHashCode()
34
{
35
return Id.GetHashCode() ^ (FirstName == null ? 0 : FirstName.GetHashCode()) ^ (LastName == null ? 0 : LastName.GetHashCode());
36
}
37
}
Cached Result
hello word