I have some questions please for the following pieces of source code. It's something new for me and I don't understand a lot of things.
Let's get started!
public class Person : IEquatable<Person>
{
public string Name { get; set; }
public int Age { get; set; }
public bool Equals(Person other)
{
if (other == null) return false;
return (this.Name == other.Name
&& this.Age == other.Age);
}
}
public class PersonComparer : IEqualityComparer<Person>
{
public bool Equals(Person x, Person y)
{
if (x == null || y == null) return false;
return (x.Name == y.Name && x.Age == y.Age);
}
public int GetHashCode(Person obj)
{
if (obj == null) return 0;
return obj.Name.GetHashCode()
^ obj.Age.GetHashCode();
}
}
In the second class PersonComparer I don't understand the GetHashCode function, what it does and especially this line of code
return obj.Name.GetHashCode()
^ obj.Age.GetHashCode();
<pre>
void Main()
{
var list3 = new List<Person>
{
new Person { Name = "Alice", Age = 25 },
new Person { Name = "Bob", Age = 30 },
new Person { Name = "Charlie", Age = 35 },
new Person { Name = "Eve", Age = 50 }
};
var list4 = new List<Person>
{
new Person { Name = "Bob", Age = 30 },
new Person { Name = "David", Age = 35 },
new Person { Name = "Eve", Age = 40 }
};
var Lambda = list3.Select(x=> list4.Where(y=> x.Equals(y))).Where(w=> w.Any()).Single();
Var list3list4CommonEquatable = list3.Intersect(list4);
var comparer = new PersonComparer();
var commonPeople = list3.Intersect(list4, comparer);
}
Second question :
if you try this, you will see that with following statement, I am able, to get the intersection results between list3 and list4.
var Lambda = list3.Select(x=> list4.Where(y=> x.Equals(y))).Where(w=> w.Any()).Single();
But this statement
Var list3list4CommonEquatable = list3.Intersect(list4);
doesn't work at all to give me the common elements between list3 and list 4
Why does this happen and could we correct it? It is supposed to be working, but it doesn't.
And Last question about this:
var comparer = new PersonComparer();
var commonPeople = list3.Intersect(list4, comparer);
How does a comparer work? What exactly it does? And why Equals function takes place when we call Intersect? If call an exception for example would Equals function take place in step over running?
I found these piece of code in the site
Linq Intersect() - get common elements from 2 lists[
^] and I feel confused to understand in order to use it. So any explanation will be much appreciated.
Thank you so much in advanced.
What I have tried:
<pre>public class Person : IEquatable<Person>
{
public string Name { get; set; }
public int Age { get; set; }
public bool Equals(Person other)
{
if (other == null) return false;
return (this.Name == other.Name
&& this.Age == other.Age);
}
}
public class PersonComparer : IEqualityComparer<Person>
{
public bool Equals(Person x, Person y)
{
if (x == null || y == null) return false;
return (x.Name == y.Name && x.Age == y.Age);
}
public int GetHashCode(Person obj)
{
if (obj == null) return 0;
return obj.Name.GetHashCode()
^ obj.Age.GetHashCode();
}
}
void Main()
{
var list3 = new List<Person>
{
new Person { Name = "Alice", Age = 25 },
new Person { Name = "Bob", Age = 30 },
new Person { Name = "Charlie", Age = 35 },
new Person { Name = "Eve", Age = 50 }
};
var list4 = new List<Person>
{
new Person { Name = "Bob", Age = 30 },
new Person { Name = "David", Age = 35 },
new Person { Name = "Eve", Age = 40 }
};
list3.Dump();
list4.Dump();
var Lambda = list3.Select(x=> list4.Where(y=> x.Equals(y))).Where(w=> w.Any()).Single();
list3.GetHashCode().Dump("HashCode");
list3.ToHashSet().Dump("ToHashSet");
list3.FirstOrDefault().GetHashCode().Dump("frstHashCode");
list3.FirstOrDefault().Name.GetHashCode().Dump("frstNameHashCode");
list3.FirstOrDefault().Age.GetHashCode().Dump("frstAgeHashCode");
var test =list3.FirstOrDefault().Name.GetHashCode()
^ list3.FirstOrDefault().Age.GetHashCode();
test.Dump("test");
var list3list4CommonEquatable = list3.Intersect(list4);
list3list4CommonEquatable.Dump("list3list4CommonEquatable");
Lambda.Dump("Lambda");
var comparer = new PersonComparer();
var commonPeople = list3.Intersect(list4, comparer);
commonPeople.Dump("commonPeople");
}