1 00:00:00,780 --> 00:00:03,920 Make sure you've got our Birdwatcher assembly loaded for this video. 2 00:00:03,920 --> 00:00:09,950 The next set of operators are called generation operators. 3 00:00:09,950 --> 00:00:13,370 We can use Lync to generate sequences for us. 4 00:00:13,370 --> 00:00:15,720 The first one we'll use is the range operator. 5 00:00:17,380 --> 00:00:20,740 Have you ever seen a for loop that iterates over numbers and 6 00:00:20,740 --> 00:00:22,520 adds them to a list? 7 00:00:22,520 --> 00:00:24,425 Kind of like this. 8 00:00:24,425 --> 00:00:32,591 var numbers = new List(); and 9 00:00:32,591 --> 00:00:41,228 then for (int i = 0; i < 10; i++). 10 00:00:41,228 --> 00:00:49,921 Then numbers.Add 11 00:00:49,921 --> 00:00:55,354 (i); and 12 00:00:55,354 --> 00:01:00,245 numbers. 13 00:01:00,245 --> 00:01:04,222 The range operator in LINQ can do that for us in one line. 14 00:01:04,222 --> 00:01:12,343 Like this, var numbers = Enumerable.Range( and 15 00:01:12,343 --> 00:01:16,404 then the starting number. 16 00:01:16,404 --> 00:01:22,030 And how many times it should add a number to the list. 17 00:01:25,000 --> 00:01:28,360 And it's the exact same thing, so way faster type. 18 00:01:28,360 --> 00:01:30,900 It only works with integers though. 19 00:01:31,930 --> 00:01:35,223 That first parameter is the starting number and 20 00:01:35,223 --> 00:01:39,266 then the second is how many numbers to add to the sequence. 21 00:01:39,266 --> 00:01:43,272 So we could start at ten and 22 00:01:43,272 --> 00:01:47,459 add ten numbers like this, 23 00:01:47,459 --> 00:01:55,668 var numbers = Enumerable.Range (10, 10). 24 00:01:55,668 --> 00:02:00,510 And now it can be a useful shortcut. 25 00:02:00,510 --> 00:02:03,550 The next one is called repeat. 26 00:02:03,550 --> 00:02:06,110 We can use it with any type of element. 27 00:02:06,110 --> 00:02:07,430 Let's clear the console first. 28 00:02:10,440 --> 00:02:14,622 So I'm going to call Enumerable.Repeat. 29 00:02:14,622 --> 00:02:17,560 Then I'm gonna pass it a string. 30 00:02:17,560 --> 00:02:23,036 And we'll say, LINQ is awesome. 31 00:02:23,036 --> 00:02:25,760 And then we need to tell it how many times to repeat. 32 00:02:27,990 --> 00:02:32,050 All right we've got ten strings that say LINQ is awesome. 33 00:02:33,650 --> 00:02:37,189 Let's try it with some blank bird objects. 34 00:02:37,189 --> 00:02:46,903 var blankBirds = Enumerable.Repeat(new Bird(), 35 00:02:46,903 --> 00:02:52,373 and we'll give it five of them. 36 00:02:52,373 --> 00:02:53,765 All right, let's check those out. 37 00:02:53,765 --> 00:02:58,150 blankBirds, and we've got five blank birds. 38 00:02:59,320 --> 00:03:03,330 The next operator doesn't generate any elements in a sequence, but 39 00:03:03,330 --> 00:03:06,500 it creates an empty sequence instead. 40 00:03:06,500 --> 00:03:09,113 So let's try that out. 41 00:03:09,113 --> 00:03:17,112 var emptyBirds = Enumerable.empty. 42 00:03:17,112 --> 00:03:22,226 But it's generic and we need to tell it what type, cuz otherwise it wouldn't know. 43 00:03:25,715 --> 00:03:28,770 It can be useful for when you need an empty innumerable. 44 00:03:28,770 --> 00:03:32,729 You can't instantiate an Enumerable by itself like var 45 00:03:32,729 --> 00:03:39,252 emptyBirds = new Enumerable. 46 00:03:39,252 --> 00:03:40,300 Nope. 47 00:03:40,300 --> 00:03:43,260 The next operator is an interesting one, 48 00:03:43,260 --> 00:03:46,960 the DefaultifEmpty operator when used on a sequence that is 49 00:03:46,960 --> 00:03:51,460 empty will actually return a sequence loaded with one default value. 50 00:03:52,570 --> 00:03:56,250 This is handy when we need to make sure we're dealing with a collection that has 51 00:03:56,250 --> 00:03:58,350 at least one item in it. 52 00:03:58,350 --> 00:04:01,330 Let's take an empty sequence of integers. 53 00:04:01,330 --> 00:04:05,350 Let me clear the console real quick, get rid of that ugly error. 54 00:04:06,600 --> 00:04:12,720 Let's call it var numbers = Enumerable.Empty, 55 00:04:12,720 --> 00:04:15,880 this time we need an int. 56 00:04:19,040 --> 00:04:20,630 I forgot the S. 57 00:04:20,630 --> 00:04:21,570 Let's change that. 58 00:04:21,570 --> 00:04:22,640 var numbers. 59 00:04:24,150 --> 00:04:28,340 Okay, so numbers, empty enumerable. 60 00:04:28,340 --> 00:04:34,010 Then, if we use numbers and call DefaultifEmpty, 61 00:04:34,010 --> 00:04:37,670 what do you think it will return? 62 00:04:38,920 --> 00:04:43,410 A sequence that's loaded with a default value of zero. 63 00:04:43,410 --> 00:04:46,250 That's because the integer's default value is zero. 64 00:04:47,635 --> 00:04:51,440 DefaultIfEmpty is often used with the Group Join operator, and 65 00:04:51,440 --> 00:04:53,590 we'll see an example of this in a later video.