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
Learn about generation operators in LINQ: Range, Empty, and DefaultIfEmpty.
LINQ Methods
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
Make sure you've got our Birdwatcher
assembly loaded for this video.
0:00
The next set of operators
are called generation operators.
0:03
We can use Lync to generate sequences for
us.
0:09
The first one we'll use
is the range operator.
0:13
Have you ever seen a for
loop that iterates over numbers and
0:17
adds them to a list?
0:20
Kind of like this.
0:22
var numbers = new List<int>(); and
0:24
then for (int i = 0; i < 10; i++).
0:32
Then numbers.Add
0:41
(i); and
0:49
numbers.
0:55
The range operator in LINQ can do that for
us in one line.
1:00
Like this,
var numbers = Enumerable.Range( and
1:04
then the starting number.
1:12
And how many times it should
add a number to the list.
1:16
And it's the exact same thing,
so way faster type.
1:25
It only works with integers though.
1:28
That first parameter is
the starting number and
1:31
then the second is how many
numbers to add to the sequence.
1:35
So we could start at ten and
1:39
add ten numbers like this,
1:43
var numbers = Enumerable.Range (10, 10).
1:47
And now it can be a useful shortcut.
1:55
The next one is called repeat.
2:00
We can use it with any type of element.
2:03
Let's clear the console first.
2:06
So I'm going to call Enumerable.Repeat.
2:10
Then I'm gonna pass it a string.
2:14
And we'll say, LINQ is awesome.
2:17
And then we need to tell it
how many times to repeat.
2:23
All right we've got ten strings
that say LINQ is awesome.
2:27
Let's try it with some blank bird objects.
2:33
var blankBirds =
Enumerable.Repeat(new Bird(),
2:37
and we'll give it five of them.
2:46
All right, let's check those out.
2:52
blankBirds, and
we've got five blank birds.
2:53
The next operator doesn't generate
any elements in a sequence, but
2:59
it creates an empty sequence instead.
3:03
So let's try that out.
3:06
var emptyBirds = Enumerable.empty.
3:09
But it's generic and we need to tell it
what type, cuz otherwise it wouldn't know.
3:17
It can be useful for
when you need an empty innumerable.
3:25
You can't instantiate
an Enumerable by itself like var
3:28
emptyBirds = new Enumerable.
3:32
Nope.
3:39
The next operator is an interesting one,
3:40
the DefaultifEmpty operator
when used on a sequence that is
3:43
empty will actually return a sequence
loaded with one default value.
3:46
This is handy when we need to make sure
we're dealing with a collection that has
3:52
at least one item in it.
3:56
Let's take an empty sequence of integers.
3:58
Let me clear the console real quick,
get rid of that ugly error.
4:01
Let's call it var numbers
= Enumerable.Empty,
4:06
this time we need an int.
4:12
I forgot the S.
4:19
Let's change that.
4:20
var numbers.
4:21
Okay, so numbers, empty enumerable.
4:24
Then, if we use numbers and
call DefaultifEmpty,
4:28
what do you think it will return?
4:34
A sequence that's loaded with
a default value of zero.
4:38
That's because the integer's
default value is zero.
4:43
DefaultIfEmpty is often used with
the Group Join operator, and
4:47
we'll see an example of
this in a later video.
4:51
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