Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Methods can be generic too!
-
0:00
We just saw how we can write stand-alone generic methods
-
0:03
when we wrote the take method.
-
0:05
The cool thing about generic methods is that C#
-
0:08
can infer what the type perimeter is so we don't have to type it here.
-
0:14
That's one of my favorite things about generic methods, we can use this feature
-
0:19
to simplify instantiating the EnumerableCompositor Class.
-
0:28
So, let's go back to EnumerableCompositor.cs.
-
0:32
First, we'll create another class name, EnumerableCompositor, but
-
0:36
this time it won't be generic.
-
0:40
Will also make it a static class since it will only contain static methods.
-
0:45
So, say static class EnumerableCompositor one
-
0:54
common pattern is to write a method that takes care of the instantiation for us.
-
0:59
This is known as a factory method.
-
1:01
So, save public, static, and it will return and in EnumerableCompositor.
-
1:07
So, say in EnumerableCompositor the generic one T.
-
1:16
i will name the method create, since it will be used to
-
1:20
create an EnumerableCompositor.
-
1:24
We'll specify the type parameter here and it will accept and IEnumerable
-
1:32
of T, and will just have it, except an array of IEnumerables.
-
1:40
We'll call it collections and
-
1:43
it will return a new instance of EnumerableCompositor and
-
1:49
we'll just pass in collections here.
-
2:01
So, this create method is taking an array of collections and then
-
2:06
passing that array of collections to the constructor of a EnumerableComposite of T.
-
2:12
To create a new instance and then returning it.
-
2:16
Now, in main to get and EnumerableCompositor we can change this
-
2:21
to EnumerableComposite.Create and
-
2:30
we'll pass in our array of collections.
-
2:35
So, say new IEnumerable.
-
2:49
And I'll split this up on two lines we can see it easier.
-
2:54
And this is an array.
-
2:57
Because create is a generic method C# was able to infer what the type parameter is.
-
3:04
However, we lost the ability to use the collection initializer instead we have to
-
3:10
pass in array of collections we want the EnumerableCompositor to be composed of.
-
3:15
C# has another neat feature that allows passing
-
3:19
any number of parameters into a method.
-
3:22
Back in the create method,
-
3:23
we can add a params keyword here in front of the collections parameter.
-
3:35
Now we can go back to where we're calling create.
-
3:38
Notice that there are no red squiggly lines.
-
3:41
And we can still pass in an array.
-
3:44
However, because we added the params keyword,
-
3:47
we can also just list the collections here as if they're normal parameters.
-
3:56
The params keyword allows passing in any number of parameters that are of the type
-
4:01
in the array.
-
4:02
There's a caveat though, only the last parameter of the method
-
4:06
can have the params keyword In this case we only have a single
-
4:11
parameter in the create method and it's an array of a IEnumerable T.
-
4:16
So when calling create we can pass in as many IEnumerable of T,
-
4:20
as we want just as if there are normal parameters to the method.
-
4:24
They all get put in a single array which we can treat just like a regular rate.
-
4:28
In this case we're just passing the array to the constructor
-
4:32
of EnumerableCompositor.
-
4:34
With the static create method we can now create an instance of
-
4:39
innumerable compositor without specifying the type or using the new keyword word.
-
4:44
This is so streamlined that we can copy this right into the for each loop.
-
4:58
EnumerableCompositor dot create is still a lot to type just to wrap
-
5:03
a bunch of collections.
-
5:05
So that they can be enumerated as a single collection.
-
5:08
Let's make it, so that we don't have to type in EnumerableCompositor any more.
-
5:13
We do that with a using static directive at the top of the file.
-
5:18
So we can say, using static Generics Demo.EnumerableCompositor.
-
5:28
This means that we can call static methods in the EnumerableCompositor class
-
5:33
without prefixing them with the class name.
-
5:40
So, we can change this to just create.
-
5:44
But without the class name it's hard to tell what we're creating here.
-
5:48
Let's just change the name of the create method to EC.
-
5:53
In Visual Studio, I can do a rename by clicking on it and hitting control RR.
-
5:59
I'll type in EC here.
-
6:02
Notice that it also changed back in the method declaration.
-
6:05
Now, this is all we need to create and new EnumerableCompositor.
-
6:10
And because in EnumerableCompositor is IEnumerable we can use
-
6:15
any link method on it.
-
6:17
So, to count all of the odd integers contained in all of these collections
-
6:21
we can just do this down here.
-
6:37
By leveraging the power of IEnumerable, Yield and Generics,
-
6:42
we've been able to create a very elegant solution
-
6:45
to a common problem that can be used for any group of collections.
-
6:50
Now, instead of writing for foreach loops that all have duplicate code in them,
-
6:55
we've been able to reduce it to a single line of code that's very readable.
-
7:00
I like the core logic of my code to be as simple and readable as possible, so
-
7:04
I find myself making lots of methods in classes like these
-
7:08
to pull out anything that isn't part of the core logic.
-
7:12
These methods and classes, like the EnumerableCompositor class and
-
7:16
the EC method, can often be re-used.
-
7:19
They get added to my toolkit and
-
7:21
I often find myself using them again in later projects.
-
7:25
This often means writing custom data structures by implementing IEnumerable or
-
7:30
another generic interface such as ICollection, IList, ISet or IDictionary.
-
7:35
All of those interfaces inherit from IEnumerable.
-
7:39
So, you will use the same principles we've discussed here
-
7:42
when implementing those interfaces as well, we're not done yet.
-
7:46
No discussion of generics is complete without learning how to limit
-
7:50
what can be used as a generic parameter.
-
7:53
We'll learn about generic constraints in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up