Let's explore the constraints one more time with a more real world ish example.
This is something that you would face when using Entity Framework.
For example, you will find similar structures, so that one would be pretty cool.
Now I just have the program CSS right here.
Let's just create a new C sharp class and simply call it repository.
Great.
So for your interest, if you have never heard that word, a repository is basically just something
where you can store things inside.
You know that maybe from a GitHub repository inside of a GitHub repository, you can find the source
code inside of a repository.
There could also be like any kind of data storage, right, for a database or whatsoever.
So we can store things inside of repository.
Great.
So what we want to do here is we first of all want to make this internal class a generic one.
So we specify a type T.
However we always want to make sure that in our application whatever we save to that repository has
an ID right.
So an ID is mandatory.
Let's create an interface I will do that right here above that class.
You know usually you would create a new file for that.
But I want to keep the code as simple as possible for you.
So let's create an interface internal.
You can also use public right interface and let's call it I entity.
There we go.
And inside of I entity we have at least a property called id.
So right down int id.
And let's make it a get only.
There we go.
So we have an interface.
And in that interface we specify that whatever implements that interface it has to have an ID property.
Now we can use this interface here inside of our generic repository class declaration.
So again we want to say where they go where T is implementing our I entity interface.
Right.
So whatever type we submit here for our generic repository that type has to implement the interface.
That's the constraint already.
We will see that in action.
Right now let's just create a private list of our type T here.
Right.
And we can say this is our values.
Totally fine.
Let's initialize a new list of type T right.
And then we simply add a you know it's just for demonstration purposes.
Let's just create a method here public void add.
And then we simply add a T entity.
Right.
And finally the logic is that we take our values dot add and we add the entity from the parameter.
There we go.
That's just what our repository can do, right?
We have our values inside of the repository.
And we have a simple add method.
Now this is using our generic type T.
You've already learned all of what's going on here in the past lectures.
Great.
But now the crucial part is the actual instance.
So let's switch now to our program.cs.
Let's say we want to store products inside of our repository.
All right.
Therefore we want to create a new class.
Again I will put that right here class product.
Now as I've said, if we want to store it in our generic repository, we have to implement the element
that we want to store in that repository.
Right?
We have to make sure it's implementing the I entity interface because we have set up this constraint.
So if we want to save products into our generic repository, we have to make sure that our data type
class product here implements the I entity.
All right.
Let's set it up this way.
And since we're using our I entity interface here we have to implement the public int id as get.
For example.
You can also do it set.
Now the error is gone because we have implemented what we have to implement from our I entity interface.
Right.
And now let's scroll up and let's try to create a repository.
First let's write down repository of type product.
Right.
Let's call it repository.
And it's a new repository of type product.
Now let's create a new product.
So var product equals to new product.
And you we cannot change the ID right.
Because it's a get only.
Let's just assume that any kind of framework will set the ID or you will write any internal logic in
the end which will assign a new ID.
You should never manually set an ID, right?
We should have any logic for that and var product.
And now we can take our repository and add this product to it.
Why are we able to do that?
Well because we are passing our constraint that we set here because we submit as the type T for our
repository, the product class and the product.
Loss is implementing the I entity interface, which makes sure that we have an ID, right?
And in that way the constraint is passed.
If we would now go ahead and remove that I entity interface, you can immediately see that it is not
matching anymore.
We are not able to do that.
We face a compile time error saying that we have to use our interface again.
Great.
So in that way, this is a more real world approach on using a constraint inside of your C sharp applications.
And you will gain type safety.
Again, more security and also more predictability and maintainability.
Well, so that was quite a lot already.
But still there is more to discover on generics.
So let's move on to the next lesson.