1 00:00:00,150 --> 00:00:02,148 In the split method we just coded, 2 00:00:02,148 --> 00:00:06,130 we checked to make sure that this parameter isn't null. 3 00:00:06,130 --> 00:00:09,850 Sometimes we want the parameter to be able to be null. 4 00:00:09,850 --> 00:00:15,060 For example there's a method called is null or empty in the string class. 5 00:00:15,060 --> 00:00:16,810 Let's go take a look at that. 6 00:00:16,810 --> 00:00:22,630 Here's the is null or empty method in the string class and notice that it's static. 7 00:00:22,630 --> 00:00:28,190 The reason it's static is because if the string is null, 8 00:00:28,190 --> 00:00:33,350 you can't call a method on it, otherwise you'll get a null reference exception, 9 00:00:33,350 --> 00:00:38,000 but with extension methods you can call a method on something that is null. 10 00:00:38,000 --> 00:00:43,900 So let's write our own version of the is null or empty method for string. 11 00:00:43,900 --> 00:00:46,670 So say public static and 12 00:00:46,670 --> 00:00:50,100 notice that I'm doing this in the string extension static class again. 13 00:00:51,490 --> 00:00:57,460 In return bool and the name is null or empty. 14 00:00:59,180 --> 00:01:02,010 It takes, this extend string. 15 00:01:03,070 --> 00:01:07,699 We'll just call it this, and inside here 16 00:01:07,699 --> 00:01:12,727 we'll just call string.is null or empty and 17 00:01:12,727 --> 00:01:17,640 pass in this and we'll want to return that. 18 00:01:19,980 --> 00:01:22,120 Now let's go over to program.cs and try it out. 19 00:01:23,210 --> 00:01:25,700 So here we have our null myString. 20 00:01:26,750 --> 00:01:33,400 So I can just call myString.IsNullOrEmpty like so. 21 00:01:33,400 --> 00:01:37,380 Now, one thing you may not know about the string class is that 22 00:01:37,380 --> 00:01:41,730 the string class actually implements the IEnumerable interface. 23 00:01:43,060 --> 00:01:43,580 Let's take a look. 24 00:01:48,100 --> 00:01:53,080 As you can see, a string is an I numerable of char. 25 00:01:53,080 --> 00:01:58,060 So it could actually move our is null or empty extension method into 26 00:01:58,060 --> 00:02:03,700 the I Enumerable extensions class and extend all I Enumerables. 27 00:02:03,700 --> 00:02:05,130 Let's see how that would look. 28 00:02:05,130 --> 00:02:07,958 First I'll create a new class to contain this extension method. 29 00:02:13,882 --> 00:02:17,437 I'll call it IEnumerable extensions. 30 00:02:22,904 --> 00:02:23,860 Make it public. 31 00:02:24,870 --> 00:02:25,530 Make it static. 32 00:02:29,410 --> 00:02:30,300 And we'll copy. 33 00:02:33,830 --> 00:02:35,670 And I'm not gonna delete it from here. 34 00:02:35,670 --> 00:02:43,507 I'm actually just gonna copy it from here. 35 00:02:43,507 --> 00:02:50,286 We'll copy that over here. 36 00:02:50,286 --> 00:02:52,871 We will change this so that is null or 37 00:02:52,871 --> 00:02:55,980 empty can be called on any I numerable of T. 38 00:02:57,230 --> 00:03:01,216 So say this I 39 00:03:01,216 --> 00:03:06,810 numerable of T. 40 00:03:06,810 --> 00:03:08,560 And instead of calling string. 41 00:03:08,560 --> 00:03:13,333 isnullorempty we'll 42 00:03:13,333 --> 00:03:18,356 return @this = null or 43 00:03:18,356 --> 00:03:23,140 !@this.Any(). 44 00:03:23,140 --> 00:03:30,170 The Any method is actually a extension method provided by system.link. 45 00:03:30,170 --> 00:03:33,790 If you're familiar with the link you may know that link 46 00:03:33,790 --> 00:03:39,130 relies heavily on extension methods and any is one of those extension methods. 47 00:03:39,130 --> 00:03:42,160 There is something else subtle happening here, this or 48 00:03:42,160 --> 00:03:48,480 condition is actually taking advantage of a feature called short circuiting. 49 00:03:48,480 --> 00:03:52,180 Remember calling Any on this variable 50 00:03:52,180 --> 00:03:56,730 will throw a null reference exception if this is null, but 51 00:03:56,730 --> 00:04:02,390 if this is null then the first part of this condition will be true. 52 00:04:02,390 --> 00:04:06,170 Short circuiting means that the first condition of an or 53 00:04:06,170 --> 00:04:12,120 that evaluates to true will cause the entire expression to be true. 54 00:04:12,120 --> 00:04:15,940 If that happens, any of the later conditions or 55 00:04:15,940 --> 00:04:19,560 expressions in the condition, are no longer evaluated, 56 00:04:19,560 --> 00:04:24,200 because C Sharp already knows that the expression should be true. 57 00:04:24,200 --> 00:04:28,000 So if this is null, then this code right here will never be called. 58 00:04:29,140 --> 00:04:32,790 We're now in a very interesting situation now. 59 00:04:32,790 --> 00:04:39,420 We now have two methods that are named the same that can be called on the same type. 60 00:04:39,420 --> 00:04:40,950 Here I'll demonstrate to you. 61 00:04:43,660 --> 00:04:47,630 Here we have myString.IsNullOrEmpty. 62 00:04:47,630 --> 00:04:50,462 Let's see what happens if we comment out 63 00:04:50,462 --> 00:04:55,200 the isNullorempty method in the string extensions class. 64 00:04:57,500 --> 00:05:01,530 If we go back to Programm.cs we don't see any red squiggly lines. 65 00:05:02,820 --> 00:05:05,075 And this will compile just fine. 66 00:05:07,163 --> 00:05:08,720 See the build succeeded. 67 00:05:09,890 --> 00:05:14,790 So I wonder which is null or empty method will be called. 68 00:05:14,790 --> 00:05:18,580 To test this out, let's go back here and uncomment this method. 69 00:05:18,580 --> 00:05:20,410 And then let's put a break point inside of it. 70 00:05:22,760 --> 00:05:26,720 Let's also put a break point inside the IEnumerable extensions version. 71 00:05:28,610 --> 00:05:29,430 Let's go ahead and run this. 72 00:05:32,050 --> 00:05:34,180 Notice that the is null or 73 00:05:34,180 --> 00:05:39,300 empty method inside of the string extensions class was called. 74 00:05:39,300 --> 00:05:41,650 Instead, of the is null or 75 00:05:41,650 --> 00:05:45,650 the empty method inside of the IEnumerable extensions class. 76 00:05:45,650 --> 00:05:49,510 So how did C Sharp know which method should be called? 77 00:05:49,510 --> 00:05:54,320 Well, C Sharp knows that my string is of type stream. 78 00:05:54,320 --> 00:05:57,190 It's also of type IEnumerable of char. 79 00:05:59,030 --> 00:06:02,334 But string is a more specific type. 80 00:06:02,334 --> 00:06:08,140 So when C Sharp determines which extension method should be called, 81 00:06:08,140 --> 00:06:12,300 it always calls the one that deals with the more specific type. 82 00:06:13,870 --> 00:06:17,870 Meaning it's the farthest down in the inheritance chain. 83 00:06:17,870 --> 00:06:21,073 We can see another example of this in link. 84 00:06:22,539 --> 00:06:27,587 Let's go back to the browser, And search for 85 00:06:27,587 --> 00:06:32,274 the system.collections.generic. 86 00:06:33,913 --> 00:06:36,409 .list documentation. 87 00:06:42,915 --> 00:06:47,290 List contains a method called contains, 88 00:06:47,290 --> 00:06:50,915 which takes a parameter of type T. 89 00:06:50,915 --> 00:06:54,890 But link also provides a method called contains. 90 00:06:56,430 --> 00:06:59,180 Here these are all of the methods provided by 91 00:06:59,180 --> 00:07:02,000 link that can be used on the list class. 92 00:07:02,000 --> 00:07:06,631 These are all extension methods, the contains method provided by link and 93 00:07:06,631 --> 00:07:11,370 the contains method provided by the list class have identical signatures. 94 00:07:11,370 --> 00:07:16,230 The C-sharp knows that it should call contains on a list when list is passed in 95 00:07:16,230 --> 00:07:20,090 and it should call links contains when anything else is passed in. 96 00:07:21,110 --> 00:07:24,960 Let's create another extension method for IEnumerable. 97 00:07:24,960 --> 00:07:28,970 A very common extension method that many people use on IEnumerable 98 00:07:28,970 --> 00:07:33,250 that isn't provided by link is the is empty method. 99 00:07:33,250 --> 00:07:37,354 So say public static bool is empty. 100 00:07:39,083 --> 00:07:43,311 This IEnumerable of type 101 00:07:43,311 --> 00:07:48,650 T You need to provide the type parameter here. 102 00:07:54,240 --> 00:08:00,356 And all this will do is return if there are not any 103 00:08:00,356 --> 00:08:05,880 items in the IEnumerable. 104 00:08:05,880 --> 00:08:10,250 Now it isn't unimaginable that Microsoft could in the future 105 00:08:10,250 --> 00:08:12,990 add this identical method to .net. 106 00:08:12,990 --> 00:08:15,920 After all it is a pretty obvious method to have. 107 00:08:15,920 --> 00:08:21,380 If Microsoft added this method and we upgraded to that new version of .net, 108 00:08:21,380 --> 00:08:25,830 and we were using our own extension method called is empty. 109 00:08:25,830 --> 00:08:29,798 We get a compiler error telling us that there are ambiguous methods and 110 00:08:29,798 --> 00:08:33,185 the compiler doesn't know which one to use. 111 00:08:33,185 --> 00:08:39,425 In that case, all we need to do is go back and remove our is empty method. 112 00:08:39,425 --> 00:08:43,015 Extension methods should have a very straightforward and 113 00:08:43,015 --> 00:08:44,805 obvious implementation. 114 00:08:44,805 --> 00:08:47,125 This makes them more reusable. 115 00:08:47,125 --> 00:08:49,925 But it also helps protect against the situation 116 00:08:49,925 --> 00:08:54,270 where a method with the same name is added to the class in the future. 117 00:08:54,270 --> 00:08:57,750 If you implement them the way you would expect them to be implemented 118 00:08:57,750 --> 00:09:00,950 it's more likely that they will have the same behavior and 119 00:09:00,950 --> 00:09:04,220 you can just remove your implementation of the extension method.