You have already learned how to set up a constraint on a class on a generic class.
Let's see how we can set up a constraint on a generic method inside of a default C-sharp class.
So again, let me just create a new C-sharp class and I will call comparator.
Comparator.
Yes.
There we go.
So our internal class comparator is the usual C sharp class.
It's not generic right here we have a public static bool are equal.
We'll call it that way.
And then we have a generic or generic angle brackets type T for example.
Then we are comparing two parameters right.
So t first t second.
We can just do it that way.
There we go.
And we are simply returning if first equals second.
As you can see we are trying to compare those two elements here.
Right.
But the problem is that this only works for reference types.
Therefore we have to set up a constraint and in our method.
So simply write down where.
Then T is a class.
There we go.
And it works already.
So the method declaration gets a bit long.
But here we have a public static bool.
So we can call this method from everywhere without an instance.
This is what static is doing.
We are returning a bool.
We are checking if two elements to generic types are equal, basically the first and the second, and
we have to make sure that we can compare it.
And this kind of comparison only works if we have reference types.
That's a whole different part, right?
Just forget about that.
The part is that we can set constraints for our generic methods too, just like we did for generic classes.
In our case, we simply make sure that whatever you submit here as first and second, right?
That generic type is a class.
So is a reference type.
Alrighty.
And now if we save it and we go to our compiler or program class here and we use compare dot are equal.
As I said we can call it because it's static.
We cannot pass in one and one.
That does not work because one is an integer and an integer is a value type, not a reference type.
But we could, for example, simply set up a public class product right?
There we go.
And then create two products here.
So var product one equals to new product right.
And then we can simply copy it paste it.
Let's say product two I know there are 100 different ways to do that.
I just want to make it as obvious as possible right.
Always keep that in mind.
And since those are well product is a class, it will pass our constraint.
And we can compare product one and product two.
And then that way we can use our comparator equal.
So if you're interested let's simply write down c w.
And let's put in whatever the result is.
So var result equals to this.
And we put in the result into the console right line.
Let's simply hit start.
And let's see if our product one and product two are equal.
And they are for sure not equal.
Yes they share the same class.
Both of them are products.
But as I said, if we create an instance here, those instances are not the same, right?
They are stored inside of our computer memory at completely different parts, right?
So they are allocated differently.
Here's the first one.
Here's the second one.
Both instances of our products are unique.
So yes, they have the same product class that they are using and relying on, but they are entirely
unique when it comes to their instance.
So they are not equal.
All right.
So that said, you now know that you can create a constraint for your generic methods to.