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