Welcome to this exciting section of the course.
My name is Yannick.
If you don't know me, I am the co-instructor of this course and I'm here to teach you a very advanced
topic of C-sharp, which is generics.
So we're using generics to build generic functionality, and let's explore what exactly that means.
So how we use generics in C-sharp and why we would use them.
First of all, you should know that we can create generic classes, methods, interfaces, and even
delegates.
You would like to have an example where I'm sure that you have used a list in C-sharp before.
Have you seen these angle brackets?
Right inside of those brackets you can specify a type.
You could for example create a list of type int right.
We could call them numbers.
We initialize a new list of type int.
We use the angle brackets and then specify a data type.
Then this data type will get used in the list.
We can also do that with a string.
For example list of type string.
We can also do that with custom data types that we have created.
List of type product for example.
So by using those angle brackets we can see that list is a generic type.
Maybe you have seen or noticed before that if you want to use a list or a dictionary you're implementing,
or you have to implement a specific namespace, the so-called System.collections.generic namespace inside
that one.
You can see this inside of the official documentation.
We will have a dictionary, for example a hash set key value pair, a linked list, and here at the
bottom also a list.
You can see that it's a list and it specifies in angle brackets a type called t.
All right.
So those right here are generics.
You know there are some more right.
But those are the most important ones or those that you have already seen before.
The question is why would you create your own generic types.
You know, you know why you want to use a list.
If you want to add something and remove something to get or to work around that array.
Limitations, for example.
And the answer is quite simple.
Just like a list, you don't want to build the entire functionality for each single data type.
Instead, you want to share functionality across multiple data types.
So if we would create a list no matter of what data type, let's assume it's type product.
You can see that right here we have add, find and remove.
Right.
And no matter what type we would specify if it would be a string, an integer product person whatsoever,
we would always have access to add, find, remove and so on.
Because list is a generic type and grants us therefore access to all of its methods.
For example, right?
No matter what data type we specify as the type of this generic.
So please keep that in mind.
We are creating generics to share functionality across multiple data types.
Now, if you would dig deeper into the original source code of the implementation of a list, you would
find it looking something like that public class list.
And then we got the angle brackets specifying a type T.
This does not have to always be called T.
It could also be t t, session t value, whatever any short name.
Usually you will find the value specified as t, which means type any generic type.
Great if we would create our own functionality, you know we have a list in C sharp implemented already,
right?
We would not need to create our own list implementation.
That would be kind of redundant.
Instead we would create some other functionality.
Let's assume we create a logger.
You know, no matter what data type this logger receives, we always want to print it out.
Right?
So public class logger of type T we could create a string logger, an integer logger, a product logger
or a person logger.
And every time we simply log what's inside that data type or what value that data type, for example,
holds.
Now here we have created a class which is generic, but we can also create generic methods.
We can put generic methods in generic classes or in default classes which are not generic.
Right.
So we can also create generic methods.
For example here we would have a public void log.
Again you know it's a method right?
It returns a void.
So basically nothing.
And then we make it a generic method by again using angle brackets specifying type T right.
And then we have a parameter again using that type specified here object to log.
Then we simply log to the console Console.WriteLine.
We use that parameter which is of type t could be any type, no matter if you use a string integer person
product, and we simply write down two string printed to the console, right?
So again we create a generic method by using the angle brackets.
Then we can specify that type t if we want to have a parameter and refer to it.
You can see that right here right.
So we can use that T if we specify it inside of our generic.
Declaration, and then we can write down the functionality.
So the logic which we want to share across all data types.
So here we say take the generic type from the parameter object to lock of type t could be string, could
be integer or whatever data type and then log it to the console.
Alright, so that's a simple example for a generic method.
Now let's just sum up what we have learned about generics so far.
You can create generic classes, structs, interfaces, methods and delegates.
So basically everything that you know could be a generic generics are meant to share functionality across
multiple data types.
Always think of a list.
No matter if you create a list of type, string or integer, you can use all the methods like adding
removing right that functionality.
You can use it on every data type that your list is using.
Then in addition to that, we create generics to reduce code duplication and to improve its maintainability.
You could imagine if we would have like a list int or an int list, or a string list or whatever as
its own data types.
Both of them would have the implementation of adding, finding, deleting, replacing.
Again for that list int.
Then we have a string list adding removing.
Right.
That doesn't make sense.
So it's easier instead of creating their own classes all the time with their implementations, creating
one generic list and then say no matter what data type you have, simply do the same over and over again.
If it's a string or integer, doesn't matter.
That's the reason why generics improve the maintainability, because we have less code to write.
And then finally, generics are used by advanced code design patterns.
For example, the repository design pattern.
You know, if you work on big software or software which is managed in a or developed in a scrum team,
for example, chances are that you will work together with some experienced senior developers which
are using some very professional software design patterns like the repository design pattern, which
we will discover along in this section.
Right.
And then you will definitely face generics.
Now there is a big pitfall when it comes to generics, and the biggest pitfall is that you could over
architect the code when a simpler solution would have done the job too.
So if you don't need to make your code generic, you don't have to do it right.
If there is no functionality to share across multiple data types, just don't do that for the sake of
it.
Only do it when you need it, and if you have no design pattern to follow along, for example, the
repository design pattern and you don't have that, think about it.
Would you like to use one a software design pattern, right?
Or are you really developing a very small application and you do not use it or don't need it?
Maybe it's not worth the overhead.
It really depends.
But that's the biggest pitfall.
And on the other side, there are some really nice upsides that we should discover now.
So you will unlock next level C sharp code if you dig deeper into generics.
So that's the best part about it.
You will be able to work on well-designed software.
You'll be able to architect software on a whole different level, and you will be even able to create
your own frameworks.
And I can promise you, if we will ever inspect pre-existing C sharp types like lists or dictionaries
or hash sets or whatsoever.
And you take a look at Visual Studio and you dig into that implementation, you will have an entirely
new understanding of what is going on in C sharp under the hood.
So that said, let's move on to the next lesson to create our own C sharp generic class.