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
Writing your own data structures to help you work with data can help keep your code clean and readable. You’ll find that the data structures you write to help you solve one problem can often be reused the next time you run into a similar problem. Coding them as generic classes makes them even more reusable.
Hello and welcome.
0:05
I'm Jeremy, in this workshop,
0:06
we'll see how to use generics to create
our own types of data structures.
0:08
Data structures are classes that help
to organize and use data efficiently.
0:13
For example, lists and dictionary are data
structures provided by the .Net Framework.
0:17
The .Net Framework refers to these
data structures as collections
0:21
because they're used to
organize collections of things.
0:26
List and dictionary are also
examples of generic classes.
0:29
You probably remember from previous
courses that we can specify
0:34
what type of object we want a list to
contain by putting the name of the type
0:38
between these two angle brackets
when we instantiate the list.
0:42
This is called the generic type parameter.
0:46
And it's what makes list a generic class.
0:49
We use generic classes like list all the
time, but in order to really understand
0:52
how generics and collections work,
it's best to implement our own.
0:56
In fact, writing your own data
structures to help you work with
1:00
data can help keep your code clean and
readable.
1:03
You'll find that data structures you
write to help you solve one problem
1:06
can often be reused the next time
you run into a similar problem.
1:10
Coding them as generic classes
makes them even more usable.
1:14
Let's start writing our own
generic data structure.
1:19
I'll be writing the code for
this workshop using Visual Studio.
1:22
If you're new to Visual Studio,
you can get familiar with it by
1:27
following the link to the tree house
workshop in the teacher's notes.
1:30
Of course, you don't have to use
Visual Studio to code in C-sharp.
1:34
You can use any editor you want or
1:36
even open a Treehouse workspace by
clicking on the button on this page.
1:39
Let's get going.
1:43
I'm in Visual Studio and I've created
a console application project.
1:45
I've named this project GenericsDemo.
1:49
In program.cs, I've created four
different collections of integers,
1:52
two lists, a HashSet and an array.
1:57
Let's say we wanted to
count all of the odd
2:00
numbers contained in
these four collections.
2:03
How could we do that?
2:06
I've already written a method to
determine if a value is odd or not.
2:07
Now for each collection, we can loop
through each value in the array and
2:11
check if it's odd.
2:16
We'll increment a counter for
each odd number we find.
2:17
Let's see what the code for
this looks like.
2:20
So I'll start by making
an integer variable and
2:24
I'll name it numOdd,
I'll set it equal to 0.
2:28
This will help us keep track of how
many odd numbers we've encountered.
2:34
Now I'll say, foreach var value in list1.
2:38
If, and then we'll call the IsOdd
method and pass in value.
2:49
So if it's odd, we'll increment numOdd,
so I'll say numOdd ++.
2:56
Now we just need to do this with
the other three collections.
3:05
So I'll copy this code, And
3:08
I'll paste it and each time, I'll
change it to the next collection name.
3:13
So this one will be list2.
3:17
I'm going to do this two more times.
3:25
So this one is for set1.
3:27
And the last one is for array1.
3:35
There we go.
3:44
So this code loops through
each collection and
3:45
increments numOdd for
each odd number that it finds.
3:48
We could do anything we wanted with
each value in these collections.
3:52
But we'd have to repeat
it inside of each loop.
3:56
There's got to be a better way.
3:58
We could shorten
the substantially using LINQ.
4:00
If you're unfamiliar with LINQ,
4:04
you'll find a link to a great course
about it in the teacher's notes.
4:05
So to use LINQ, I'll first have to go
up here and add a using directive.
4:09
So I'll say using System.LINQ.
4:13
This allows us access to all of
the extension methods defined in LINQ.
4:19
Now it's write our LINQ expression.
4:25
Our LINQ expression will replace
all of these foreach loops.
4:31
So I'll just say numOdd
equals list1.Count.
4:34
So here I'll write a lambda
expression where x is the value.
4:42
So say x, for
each x call IsOdd and pass an x.
4:46
We'll call Count on each collection and
add up the results.
4:56
So this is for list2.
5:02
And set1,
5:07
And array1.
5:15
That's certainly less code, but
5:18
we're still repeating the calls
to count and IsOdd four times.
5:21
What we'd really like to be able to
do is loop through these collections
5:25
as if they're all one collection.
5:29
It's not unreasonable to think that this
is something that would be useful for
5:32
more than just this case.
5:35
So we should write some code that
does this in a reusable way.
5:37
To do this, we'll write our own data
structure that takes a bunch of
5:42
collections and makes them
behave like a single collection.
5:45
We can make it a generic collection so
that it can be used for
5:49
not just integers, but any type of
object that a collection can contain.
5:52
We'll call this new collection type and
an enumerable composite.
5:57
And we'll start coding
it in the next video.
6:01
While we make this new collection type,
we'll also be learning the ins and
6:03
outs of generics as well as how to
implement the IEnumerable interface.
6:07
IEnumerable is the interface that
every collection type implements.
6:12
You need to sign up for Treehouse in order to download course files.
Sign up