All right, so for the next video, I have prepared something for delegates that goes deeper into how
delegates work so that you get a better understanding of why they're useful.
And before we go into multicast delegates, which shows how powerful delegates really are, I wanted
to show you more on delegates, as I said, and in order to understand that, you need to understand
generics, at least on a super, super, super simple level, like really high level, we're going to
go into generics and a lot more depth because there's a lot to know about generics.
But we are going to have a very simple example in this video so that you don't get lost in the next
video, where I'm actually going to use generics in combination with delegates.
Okay.
So Yannick has created an entire chapter on generics.
You can check that out.
It's I think it's quite independent, so you don't need to see anything else if you feel like this video
isn't deep enough for generics for you to understand the next video, you can jump to the generics chapter
and watch that and then come back to this chapter with delegates.
But I think what we're going to do in this video should be enough for you to understand the next example.
Okay, so what are generics?
Generics are a way to make your code more flexible and reusable by allowing it to work with any data
type.
Think of generics as templates that you can fill in with different types when you use them.
So why would you use generics?
First of all, for its flexibility, you can write one method, class, or interface and use it with
different data types without writing multiple versions.
Then the second point is type safety.
So generics help catch errors at compile time rather than at runtime, making your code safer.
And then number three is performance generics avoid the need for boxing and unboxing when working with
value types, which can improve performance.
And that concept is going to be something that Yannick is going to cover deeper.
So what is an example for a generic method?
Well, let's imagine you want to write a method that prints elements of an array.
You could write separate methods for each type.
So for example for ints, for strings, for whatever other types you have.
Or you could use a generic method that works with any type.
All right let's look at our example here.
So I have a little code example where I say public static void print int array.
And what it does is it takes an int array as a parameter.
And then it uses this for each loop to go through every single item inside of that array.
And it writes that item onto the console.
And then I have another method that is a public static void print string array.
So what it does is it does the same thing as this print int array, only that it now does that with
strings.
So instead of using ints it uses strings.
So we have an array that is of type string.
And then we have this for each loop that then just goes through every single item inside of that array
and prints that item.
Okay.
So if you just have two types that's fine.
Right.
But what if you don't know what type will come in and you want it to be manageable either way, then
you can use generics.
So let's build these two methods into one.
And just again imagine this would be 20 methods okay.
Because you are covering 20 different types.
What if a bool comes in?
What if a double comes in?
Or what if A or an array of type whatever class comes in.
So even classes, right.
Even objects that are more complex than just simple data types like strings and ints.
So let's create a method that will allow us to take any data type.
And in order to do that we need to use generics.
So how does a generic look like.
And we have seen generics a little bit already.
So I'm going to create a method public static.
And I use the static keyword because I'm inside of the internal class program with the static void main,
I want to be able to use the method inside of the main method, so this one will just be called print
array.
So it doesn't matter if it's a string array if it's an int array or whatever.
So here this is where the generics part comes in.
So these angle brackets t they are indicating that this is a generics method.
So a method that takes in generic data types.
So here I can just say t not task but t and then square brackets.
And then I give it a name let's say array.
And then I use this print array.
And by the way I need to add a data type.
So this will be void will not return anything.
So this print array now will also just have a for each loop which will go through every single item
and will just write every single item onto the console.
So this code here is pretty much the same as it was here, only that.
Now instead of saying string, we say t and t item in array and you can see here we use this t and we
used the tier.
So this T is just a replacement character for any data type.
So it just says whatever data type you give me what I'm going to do is I'm going to print it out.
Okay.
So this will now allow me to go ahead and print my array.
So here I can create an array of type int.
And this array will have a few numbers.
So 12345I think that's enough.
Like so.
And then I will have another array that will be a string array.
And now this string array will have one, two, three and so forth as its values.
And by the way this should be a string obviously.
So string array 123 and then maybe four as well.
Like our favorite character Jin.
So there we are.
So now what we can do is we can print the array either with our int array and that will work, or with
our string array.
And that will work as well.
Okay.
So now let's run this and we will see.
And actually let's also make sure that we have console dot read key so that we can make it run and not
stop.
And we can actually see what's up.
So 123451234 okay so you see our for each loop.
Didn't care if it got an int, if it got a string if it got whatever.
Okay.
And that's where the generics part in.
So this is a generic method that accepts a generic data type.
So whatever that means.
So in this case it's an array of whatever of string int double class, your own class, your custom
data type that you are creating, whatever it works with it.
So now instead of having this method and this method that do the trick, and I always have to call the
right method in order to do the right thing.
I could just do this here, create one method and now use it for all data types, which is pretty cool.
So that's how generics work in a nutshell.
Really simplistic.
But example, as I said, we go a lot deeper or Yannick is going a lot deeper into this topic.
Check out the chapter now if you feel like this wasn't enough, but if you feel like okay, that's good
enough, let's then jump to the next video where we're going to use generics in the example of a delegate
to get a really good feel for delegates.
Okay, so see you in the next video.