1 00:00:06,310 --> 00:00:06,700 Hello. 2 00:00:08,430 --> 00:00:15,840 In this lecture, we're going to talk about Question 50, what are Nullarbor reference types when hearing 3 00:00:15,840 --> 00:00:16,710 this question? 4 00:00:16,770 --> 00:00:18,360 You can think it is silly. 5 00:00:18,750 --> 00:00:22,980 After all, all reference types are another in Europe, right? 6 00:00:23,610 --> 00:00:25,760 Well, yes, this is true. 7 00:00:25,770 --> 00:00:30,310 And it didn't turn out with introducing the feature called Nullarbor Reference Stokes. 8 00:00:30,330 --> 00:00:35,010 We've seen our boat before we explain exactly what this feature is about. 9 00:00:35,160 --> 00:00:36,660 Let me show you some code. 10 00:00:37,020 --> 00:00:41,340 Let's say we work on an application that manages houses data. 11 00:00:41,730 --> 00:00:45,630 We use those two types house and address all around it. 12 00:00:46,230 --> 00:00:48,900 Let's see a tiny fragment of this application. 13 00:00:49,080 --> 00:00:52,650 But keep in mind that the entire code base can be huge. 14 00:00:53,950 --> 00:00:59,050 As you can see, this method simply formats a collection of houses as a single string. 15 00:00:59,560 --> 00:01:00,280 All right. 16 00:01:00,490 --> 00:01:04,420 We submit this code for code review and wait for the feedback. 17 00:01:04,810 --> 00:01:08,200 Soon after, we see a new comment looking good. 18 00:01:08,320 --> 00:01:13,090 But if the address of the house is no new reference, exception will be thrown. 19 00:01:13,390 --> 00:01:19,420 You can see Visual Studio warning about that with the green underline, it is true. 20 00:01:19,840 --> 00:01:23,530 After a short discussion with the reviewer will make a decision. 21 00:01:24,040 --> 00:01:26,770 The owner of the house should never be unauthorized. 22 00:01:27,190 --> 00:01:29,380 Also, the address can't be no. 23 00:01:29,530 --> 00:01:32,740 And the Wall Street and no can to be either. 24 00:01:33,130 --> 00:01:34,390 See what happened here. 25 00:01:34,720 --> 00:01:41,710 We made a decision that in this particular case, the Nullarbor type that is screened and address should 26 00:01:41,710 --> 00:01:43,390 actually not be available. 27 00:01:43,810 --> 00:01:44,530 All right. 28 00:01:44,650 --> 00:01:48,610 We have work to do if those properties should not be Nabu. 29 00:01:48,760 --> 00:01:51,610 We must add some logic to the constructors. 30 00:02:05,870 --> 00:02:13,660 Right now, it will be simply impossible to create a house in which the owner's name is nor not an address. 31 00:02:13,700 --> 00:02:15,620 We have no street or no. 32 00:02:16,280 --> 00:02:23,000 We made them practically not knowable, even if as reference types, technically they are Nullarbor. 33 00:02:23,630 --> 00:02:24,800 The problem is. 34 00:02:25,960 --> 00:02:33,040 Visual Studio, before it was updated to support Prop eight, was not smart enough to know that we actually 35 00:02:33,040 --> 00:02:39,790 ensure that the address is not normal, and it would keep giving us the warning about possible no reference 36 00:02:39,790 --> 00:02:40,510 exception. 37 00:02:41,020 --> 00:02:44,680 We could ignore those warnings, but it's not really a solution. 38 00:02:44,980 --> 00:02:51,340 Other developers who are not aware that the constructor is enforcing values not to be known may still 39 00:02:51,340 --> 00:02:55,690 be suspicious and will feel more comfortable with feeling their code was addressed. 40 00:02:55,690 --> 00:03:00,190 Checks for formal values and those checks can be really annoying. 41 00:03:02,230 --> 00:03:09,910 I guess you probably have seen similar cold before such gold sometimes takes a significant part of the 42 00:03:09,910 --> 00:03:15,190 entire cold base, making it bulky, hard to read and unmentionable. 43 00:03:16,030 --> 00:03:22,810 Wouldn't it just be simpler if we could agree the address and its components can be normal and that 44 00:03:22,810 --> 00:03:25,690 we promised to enforce it at the constructeur level? 45 00:03:26,320 --> 00:03:32,710 Well, the need for such agreement was exactly the reason for introducing Nullarbor reference types. 46 00:03:33,610 --> 00:03:40,600 This feature gives us the ability to declare a reference type as knowable or not knowable if a date 47 00:03:40,600 --> 00:03:42,580 is declared as not Malabu. 48 00:03:42,670 --> 00:03:48,970 The compiler will give us warnings in any context in which there is a risk that the value may actually 49 00:03:48,970 --> 00:03:49,570 be known. 50 00:03:50,200 --> 00:03:56,290 If the type is Malabu, the compiler would warn us where another reference exception could happen. 51 00:03:57,010 --> 00:04:02,110 We see sharp, old and new are the old way of declaring reference types. 52 00:04:02,320 --> 00:04:04,240 We make them, not Malabu. 53 00:04:12,320 --> 00:04:15,380 As you can see, the compiler gives us a warning. 54 00:04:15,650 --> 00:04:20,780 We declared the variable as non-Arabic string, but we assigned to it. 55 00:04:21,170 --> 00:04:27,530 This doesn't make much sense, hence the compiler warning we can fix it by declaring the variable as 56 00:04:27,710 --> 00:04:35,060 Abu the same way as we would declare Nullarbor value types by adding a question mark to the type name. 57 00:04:37,640 --> 00:04:39,470 Now the warning is gone. 58 00:04:40,010 --> 00:04:44,540 This variable is another string, so assigning new to it is fine. 59 00:04:45,260 --> 00:04:52,990 A very important note no reference types feature does not change anything in how the program is executed. 60 00:04:53,450 --> 00:04:56,580 Even none of the above values were still strong. 61 00:04:56,580 --> 00:05:03,470 No reference exceptions when all this feature only changes how compiler warnings are issued. 62 00:05:03,950 --> 00:05:10,250 All right, now that we know the essence of Nullarbor reference types, let's take another look at the 63 00:05:10,250 --> 00:05:11,140 address type. 64 00:05:12,900 --> 00:05:18,870 There are no compiler warnings here because the constructor parameters are non-Arab strings. 65 00:05:19,200 --> 00:05:20,640 But let me turn something. 66 00:05:23,030 --> 00:05:27,320 I made the St Parameter Nabu, and now we see a warning. 67 00:05:28,880 --> 00:05:34,010 It is here because we assign another parameter to a long Nullarbor property. 68 00:05:34,190 --> 00:05:39,950 It obviously doesn't make much sense, and it may make this seemingly non-negotiable property. 69 00:05:40,140 --> 00:05:43,490 No, let me show you one more case. 70 00:05:46,640 --> 00:05:50,750 Now, I removed the assignment to the street property completely. 71 00:05:51,050 --> 00:05:52,250 And here is the wording. 72 00:05:53,820 --> 00:05:55,390 Again, it makes sense. 73 00:05:55,800 --> 00:06:01,880 The street property is declared as Nonhlanhla String, but it will be null because it is not assigned 74 00:06:01,890 --> 00:06:02,640 anything. 75 00:06:03,900 --> 00:06:10,380 This actually answers quite a tricky question What is the default value for non Nullarbor reference 76 00:06:10,380 --> 00:06:10,950 types? 77 00:06:11,520 --> 00:06:16,020 Well, ironically, it is more because what else could it be? 78 00:06:17,860 --> 00:06:24,370 The great thing about the Nullarbor Reference Tabs feature is that it has good support from Visual Studio 79 00:06:24,370 --> 00:06:26,320 and other modern ideas. 80 00:06:26,920 --> 00:06:29,770 Let's take a look at the discord here. 81 00:06:30,040 --> 00:06:36,430 The warning is expected because the Nullarbor text may be null, but let me add another track. 82 00:06:42,440 --> 00:06:49,160 The warning is gone because Visual Studio knows that in this line, this parameter is not, nor as it 83 00:06:49,160 --> 00:06:50,810 has already been checked. 84 00:06:51,140 --> 00:06:57,380 Please notice that this support is not infallible and sometimes it can be tricked. 85 00:07:04,340 --> 00:07:06,890 As you can see here, I do something silly. 86 00:07:07,340 --> 00:07:10,430 I declared an array of non-Arab Australians. 87 00:07:10,670 --> 00:07:13,370 Yet by default, it is filled with knows. 88 00:07:13,910 --> 00:07:18,120 No compiler warning appears, though, even if this will troll. 89 00:07:18,140 --> 00:07:24,620 No reference exception because the first element of this error is small, but it doesn't mean this feature 90 00:07:24,620 --> 00:07:25,490 is useless. 91 00:07:25,940 --> 00:07:28,010 Let me quote John Skeet on that. 92 00:07:28,520 --> 00:07:35,420 Being able to know when things might or might not be normal, even when it's only two 90 percent confidence 93 00:07:35,630 --> 00:07:38,420 is a lot better than zero percent confidence. 94 00:07:38,900 --> 00:07:45,080 If you are curious what else Jon Stewart has to say about knowable reference types, check out the recording 95 00:07:45,080 --> 00:07:49,400 of his lecture I linked in the resources attached to this video. 96 00:07:49,940 --> 00:07:50,690 All right. 97 00:07:52,180 --> 00:07:59,110 There are use cases when we actually know better than the compiler, if something is or is not now one 98 00:07:59,110 --> 00:08:05,110 of the outstanding examples is when some field is set with the set up method in unit tests. 99 00:08:05,500 --> 00:08:09,370 If you don't know and you need, let me give you a very quick introduction. 100 00:08:09,670 --> 00:08:16,150 The method with the setup attribute will be executed before each test, so it gives us the same guarantee 101 00:08:16,150 --> 00:08:21,160 as the constructor that this class under test object will not be normal. 102 00:08:21,700 --> 00:08:26,380 Yet the compiler wants us that this field may be normal before exiting. 103 00:08:26,380 --> 00:08:33,190 The constructor C-sharp compiler doesn't know how the unit works, so it's not aware of this field will 104 00:08:33,190 --> 00:08:36,100 never be, nor when the test is executed. 105 00:08:36,670 --> 00:08:41,140 To get rid of the compiler warning, let's declare this field as Malabu. 106 00:08:44,350 --> 00:08:47,470 One warning disappeared, but another showed up. 107 00:08:48,070 --> 00:08:53,770 Now the compiler warns us that the CO2 you might be no one going to the automated on it. 108 00:08:54,190 --> 00:08:59,830 Making the code compliant with the comparison requirements about the Nullarbor reference types is a 109 00:08:59,830 --> 00:09:01,510 bit like playing Whac-A-Mole. 110 00:09:02,020 --> 00:09:04,930 One morning appears, but another pops out. 111 00:09:05,560 --> 00:09:07,690 But in this case, I know better. 112 00:09:08,050 --> 00:09:11,220 I know the setup method will be executed first. 113 00:09:11,650 --> 00:09:13,840 I want to say quite compiler. 114 00:09:13,840 --> 00:09:17,740 I know it's not Noel and exactly for this situation. 115 00:09:17,890 --> 00:09:20,950 The No Forgiving for Giving operator was introduced. 116 00:09:23,410 --> 00:09:29,770 As you can see, I simply put the exclamation mark after another reference type object, which I know 117 00:09:29,770 --> 00:09:32,260 is not now to suppress the warning. 118 00:09:32,830 --> 00:09:39,250 I can actually use this operator even if I know something is small, which is sometimes needed in unit 119 00:09:39,250 --> 00:09:39,790 tests. 120 00:09:40,240 --> 00:09:41,800 Let me show you an example. 121 00:09:42,780 --> 00:09:45,840 First, let's take another look at the House glass. 122 00:09:46,170 --> 00:09:51,540 Even if the address parameters are known, Malabu, I want to perform a nortec here. 123 00:09:51,990 --> 00:09:53,280 This is a good practice. 124 00:09:53,490 --> 00:09:59,910 After all, while still campanas there, because as we said, this feature doesn't change how the code 125 00:09:59,910 --> 00:10:02,750 works, it only issues new warnings. 126 00:10:03,210 --> 00:10:09,180 Of course, the person that caused this constructor will see a warning when passing now as a parameter. 127 00:10:09,360 --> 00:10:13,980 But what if he or she will ignore it after the house object is created? 128 00:10:14,040 --> 00:10:20,430 We want to be sure that we can really trust what was declared that the owner name and the address properties 129 00:10:20,430 --> 00:10:21,360 are not new. 130 00:10:21,840 --> 00:10:26,010 That's why it's best to enforce it once and for all in the constructor. 131 00:10:28,450 --> 00:10:34,900 This looks good after this validation, we can be sure that the owner name and the address will not 132 00:10:34,900 --> 00:10:40,570 be nor unaware in our code, we can forget about the never ending no tax. 133 00:10:41,290 --> 00:10:45,640 The only thing left to do is to add unique test for this constructor. 134 00:11:07,250 --> 00:11:13,280 These tests, drugs, if an argument, no exception will be thrown if the owner name is no. 135 00:11:13,820 --> 00:11:19,520 But even in this test scenario, the computer gives me a warning, but I know what I'm doing. 136 00:11:19,850 --> 00:11:24,590 I want this more here, so I kindly ask the compiler to give me a break. 137 00:11:27,780 --> 00:11:31,830 Again, the new forgiving operator proved to be useful. 138 00:11:32,460 --> 00:11:35,520 Now we understand how noble reference types work. 139 00:11:35,880 --> 00:11:38,970 The question that remains is when should we use them? 140 00:11:39,450 --> 00:11:41,460 Well, my advice is this. 141 00:11:41,850 --> 00:11:48,690 Think about the types in your cold, their fields and properties, as well as local variables and parameters. 142 00:11:49,200 --> 00:11:53,220 Can they ever be more or do you always ensure that they are not? 143 00:11:53,580 --> 00:11:54,640 If they can be, no. 144 00:11:54,840 --> 00:11:56,790 Make them knowable explicitly. 145 00:11:57,330 --> 00:12:03,540 This will clearly show what your intention was, and anyone working with your code will know that this 146 00:12:03,540 --> 00:12:06,510 thing may be new and needs to be checked for it. 147 00:12:07,230 --> 00:12:14,800 Also, a word of caution about migration since the old type declarations are made, not Nullarbor. 148 00:12:14,820 --> 00:12:20,760 Starting with C-sharp eight, it might mean that after updating your dotNet and C-sharp aversion, you 149 00:12:20,760 --> 00:12:24,030 will suddenly get an overwhelming number of warnings. 150 00:12:24,630 --> 00:12:29,350 Don't worry, they are a good thing and will help you migrate to this new future. 151 00:12:29,880 --> 00:12:35,070 But if you really don't want to see them, you can disable this feature in product properties. 152 00:12:36,130 --> 00:12:39,460 The migration process itself can be a bit tiring. 153 00:12:39,670 --> 00:12:47,140 Remember the whack a mole metaphor, but having no reference types can really save you out of pain arrows 154 00:12:47,140 --> 00:12:52,540 and no tax, making the code cleaner, more expressive and easier to maintain. 155 00:12:53,680 --> 00:12:59,890 Even if you don't decide to introduce this feature in an existing product due to complex migration, 156 00:13:00,190 --> 00:13:03,070 I highly recommend using it in a new code. 157 00:13:03,700 --> 00:13:08,800 You can disable or enable this feature per file or even a code fragment. 158 00:13:09,070 --> 00:13:15,220 This way, you can improve your code step by step and not drown in an ocean of warnings. 159 00:13:15,730 --> 00:13:21,010 To disable or enable this feature, we can use the Nullarbor Preprocessor Directive. 160 00:13:25,760 --> 00:13:31,520 As you can see now, the warning is not here, even if we are saying no to another string. 161 00:13:32,330 --> 00:13:39,830 Let's summarize Nullarbor Reference Types is a feature introduced weekly shop eight that enables explicit 162 00:13:39,830 --> 00:13:43,370 declaration of a reference type a snub or not. 163 00:13:43,790 --> 00:13:48,880 The companion will issue a warning when it recognizes the code in which are non-Arab. 164 00:13:48,890 --> 00:13:56,570 Object has a chance of being more or when we use Nabu reference types, we felt Nortec risking the no 165 00:13:56,580 --> 00:13:57,770 reference exception. 166 00:13:58,310 --> 00:14:02,600 This feature doesn't stand up to our way of executing C-sharp code. 167 00:14:02,870 --> 00:14:05,460 It only changes the generated wordings. 168 00:14:05,990 --> 00:14:12,350 This topic is recently quite popular during the interviews, as the future employer wants to check if 169 00:14:12,350 --> 00:14:14,990 you are up to date with modern C-sharp features. 170 00:14:15,500 --> 00:14:22,130 For example, you can hear the question What is the default value of non Nullarbor reference types? 171 00:14:22,580 --> 00:14:25,100 It doesn't seem obvious, but it is no. 172 00:14:25,760 --> 00:14:27,500 What is the purpose of the No. 173 00:14:27,540 --> 00:14:28,880 Forgiving operator? 174 00:14:29,630 --> 00:14:34,130 It allows us to suppress a comparable work related to not ability. 175 00:14:34,910 --> 00:14:42,140 Is it possible to enable or disable compiler warnings related to Norbu reference types on the file level? 176 00:14:42,500 --> 00:14:44,060 If so, how to do it. 177 00:14:44,750 --> 00:14:52,040 It is possible we can do it by using Norbu Enable and Nullarbor Disable Preprocessor directives. 178 00:14:52,700 --> 00:14:57,290 All right, thank you for your attention and enjoy using this feature.