So in this video, we're going to talk about Question 26, what is the purpose of the Gatkuoth method?
We are not yet quite done with collections in the next lecture.
We'll discuss dictionaries, but to understand dictionaries, we must first understand the good called
method.
So let's do it in this lecture.
Good hash gold is one of the few methods that belong to the system object type.
In other words, we can click on an object in C-sharp before we understand what this method does.
Let's see it in action.
For now, those values look enigmatic, but hopefully we'll understand them better later in director.
The guitar scored method is a hash function implementation for an object.
Let's see the definition of a hash function and hash function is a one way cryptographic algorithm that
maps an input of an SRS to a unique output of a fixed length of bits.
At least for me, this sounds completely cryptic.
First, let's understand what the result of this hash function is in c sharp.
It's an integer.
In simple terms, hash is a number calculated for some object from its components.
He was an object of person class and some hash calculated for it.
We'll take a closer look at how hash is actually calculated a bit later.
But for now, let's just say that it's a function of the values of fields and properties belonging to
the object.
In this case, we could, for example, associate each letter with some number, which would allow us
to translate words John and Smith to integers.
Then we would somehow combine those integers with the integer representing data of birth, and as a
result, we would have the hash code of the person.
According to this, if we created another object of the person class with named Don last name Smith
and year of birth 1987, the hash should be the same.
On the other hand, if this ever object to VAR, for example, different year of birth, its hash would
be different if we calculated the hash for the second time.
The result should be the same as it was for the first time, assuming the object was not modified.
If you have two objects that are different instances, but we consider them equal, the hash code for
both of them should be the same.
For example, if we have two points both having X equal to turn and Y equal to 20, even if they are
two different instances, we consider them equal, so their hash codes should be the same.
At this point, you probably wonder, OK, but what is the use for hash codes?
Well, there are many rules is that the work excuse enhanced collections.
This may again sound cryptic by now, but don't worry, we'll soon learn about one of the most useful
C sharps collections.
The dictionary if you use dictionaries before you know that its value is stored under a key, the key
can be any object, even a complex one.
But the dictionary needs to be able to translate it to an integer, and that's exactly what a good hash
called method comes in handy.
We'll learn more about it in the lecture about the dictionaries.
Let's go back to the hash functions that map complex objects into integers.
The very important trait of the hash function is that it should uniformly distribute its values.
This means if I call guitar called method on 100000 different objects of the point type, I should get
very little or none duplicated hash codes, but it is possible to have duplicated hash codes.
This situation is called hash called conflict, and it's perfectly normal.
Many people consider hash codes to be the identifiers of objects and think that two different objects
of the same type can't have the same hash code.
But this is not true, and it cannot be.
Let me prove it to you.
Let's again consider a point type.
It contains two fields X and Y.
Both X and Y are integers, so each of them can have a value between minimal value of int and maximal
value of Int for simplicity.
Let's say that the minimum value of the integer is minus two billion and the maximal is plus two billion.
This means we can have four billion different X coordinates and four billion different Y coordinates,
which in total gifts four billion times four billion different points.
What is 16 quintillion?
On the other hand, the hash itself is an integer, so it can only have four billion different values
so much, much less then different points.
So when creating different bonds will sooner or later simply run out of different households.
It is sometimes referred to as the balls into bins problem.
If we have more balls than beans and it what is stored in some being it must mean that in some bins
there is more than one bowl.
Let's summarize the hash function.
If I have two different objects of the same type.
Ideally, their hash codes should be different if I have plenty of different objects of the same type.
There should be as few duplicated hash codes as possible.
Finally, if I have two objects.
That I consider equal their hash codes should be the same.
Let's see some implementations of the good code method for some built in types.
Here is the implementation for the integer.
For instance, the implementation is as simple as it can ever be.
The integer value itself is a perfect hash code.
It will be the same fork to equal integers, and it will be different for two different integers.
There will be no duplicates at all because for each integer possible, the hash gold will be different.
Now let's consider the point type before we think of our own implementation of the good hash coordinated.
Let's see what is the default?
As we said, the Gatkuoth method is defined in the system object class, so I can call it on any object,
even if I did not override it.
Let's see some points.
As you can see, point one and point two are the same.
So I would like them to have the equal Hershkowitz.
Point three is different, so it should have a different housecoat.
Let's see the result.
Well, that's not what we wanted, the two first half goals should be the same, but they are not to
understand why is that?
So we must understand what's the default implementation of the good hash code method for reference types
it basis on the reference itself.
So the address of the object in memory for value types, it is calculated based on the values stored
in the object.
That explains why two points, even with the same X and Y, have different hash goals, which we declared
the point as a.
So our reference type point one and point two are two different objects with two different references.
The hash code is built based on the reference, so it is different for both of them.
So if we want to have the same hash codes for the points with the same X and Y, we can simply change
the point from a class to obstruct, which is a value type.
Now we have what we wanted, the first two points have the same harsh goals, but let's don't go back
to our gloss and let's try to implement the catcalled method ourselves.
Most of the base types in C-sharp already provide a good implementation of the good catcalled method.
Those methods are usually strongly based on pure math and are also pretty low level.
And because of that, I don't want to get into details on how they work.
Just so you have an idea, let's see.
They get such good method implementation for string.
As you can see, this is a pretty low level stuff.
Luckily for us, the hard work has already been done by others.
When defining custom types, we can simply combine the hash codes of the values stored in the object
into a single hash code.
For the Punkt class, it would look like this.
Hash cold combine takes on the object as parameters.
Let's make sure the implementation for the point works as expected.
Right.
The crash codes for points with equal and worse are the same.
Remember that we don't always need to combine all properties and fields of a type to get a Vikash gold.
For example, if we had a Social Security number in the person class, which by definition identifies
a person, it would be perfectly fine to use it as the only component of the hash code.
We always consider two person objects equal if they go with the same Social Security number and we can
ignore other fields if they were different.
It would most likely mean there is some error in the data itself, as two different people should never
have the same Social Security number.
All right.
We now know how to implement the guitar code method.
But the question is, what should we do it?
The answer is simple.
If the type is going to be used as a key of A. collection, like a dictionary or the hash table and
the default implementation is not working for us.
For reference types, we usually don't want the default code implementation as it compares objects by
references.
As we've seen with the point grass, we had two points with the same and why he had their hash codes
were different.
So when used as keys in the dictionary, they would be considered two different keys.
In this case, we usually want to override the Gatkuoth method and remember that hash called combine
can be a great help.
Of course, in some situations, caution is based on the reference itself are fine.
It all depends on the context.
Later, in the course, we'll learn about records.
Records are reference types that provide their own value base to get hash code method for value types.
It is a bit tricky.
There is a default implementation that works fine and uses the values stored in the fields and properties
of the type to calculate the hash code.
We've seen it when we turn to the point class to abstract.
The problem is that this default implementation uses reflection, and as we learned in the what this
reflection lecture, it's painfully slow.
Because of that, it's a good idea to provide a custom implementation of the good hash called method
invalid types and create, especially if they are going to be used as just collections keys out when
overriding the task code method.
It is important to also overwrite the equals method will explain the reason for it in the lecture about
dictionaries.
Let's summarize the good hash code.
Methode generates an integer foreign object based on this objects, fields and properties.
This integer, called hash, is most often used in collections like hafsat or dictionary.
During the interview, you can be asked a question which from my interviewing experience, almost no
one knows the answer to come to objects of the same type different by value of the same hash codes.
Yes, hash called duplications, also called hash called conflicts, can happen simply because the count
of distinct hash codes is equal to the amount of integer, and there are many types that can have much
more distinct objects than this count.
Because of that, there are simply not enough hash codes for each distinct object.
So at some point, they just start to repeat themselves.
Why?
It may be a good idea to provide a custom implementation of the hash called method for struct because
the default implementation basis on reflection and because of that, it is slow.
A custom implementation may be significantly faster, and if we use the struct as a key in collections
extensively, it may improve the performance very much.
All right, that's it.
About Hash will continue this topic in the next lecture where we learn about dictionaries.
Thanks for following along, and I'll see you there.