Checking for null without checking for null in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody i'm nikki in this video i'm going to show you how you can get a similar or even a better developer experience with what you would get if you were using c sharp 11's bang bang operator which was removed by the way it is not coming in c sharp 11 for any version of c sharp csharp.net we will be achieving that using a nougat package called nullgod.4d and you know how it goes with any open source package that i saw in this channel if you think what you see is cool there's a link in the description go ahead and click that link give the project a star on github it really means a lot to the people who work on the project if you like type of content and you want to see more make sure you subscribe ring the notification bell and for more training check out nickchopstas.com so let me show you what problem we're trying to solve and what problem the bank bank operator would solve so i have a net six project over here and i want to show you that i have nullable reference types turned off most people don't use this feature even though for new projects it is enabled by default i think people should be using it but because it is optional and even when enabled you have to make the warnings actively an error it's not something that people follow plus you have to make sure the library code you're using is also adhering to that and it's just kind of hard to adopt at this point but in any case i'm going to show you both versions and in this version we're going to have it turned off so when i turn it off and what the bang bang operator feature of c sharp 11 in case you didn't know would allow you to do is do this on constructor or method parameters and what this would allow you to do is basically not have to write something like this if item repository is null then throw new argument not exception with the name of the parameter now obviously this can be shorthanded in two ways first you can remove this and write arguableexception.throw if null the object and also the name of parameter name so you can do that as well and that's a line of code and you can also match it to the assignment so you can say double question mark so if this is null throw a new null argument exception so all those are options you have however arguably the bank operator would make it very easy to just say that hey this should not be null coming in and if it is null throw an exception however we did have a solution to this problem for years and that solution is in the form of a nougat package the nuget package is called nullgod.4d and it's part of the net assembly weaving for the platform so what this allows us to do is the moment i added into the project and remember now level reference types are turned off so if i go ahead and i build my project first i'm going to get a file created over here this is an xml defining that i have a weaver an assembly weaver and that is the null god weaver and it also indicates that my reference to the nuget package does not have a private assets all enabled maybe we're gonna talk in another video what these instructions mean uh for now it doesn't really matter all i wanna do is just set it and remove it and now what happens is that if i go ahead in the program.cs and i say item service over here equals new item service and i passed down null as an item repository and say right line then if i debug this project then the debugger as you can see stopped here and what happens when i execute is it says that null guard which is the package we're using repository is null so implicitly this thing cannot be null i didn't have to add bang bang i didn't have to add an argument no exception the nuget package inspected my code and weaved the assembly after compilation to inject that check automatically and it is as you can see here an argument null exception i can also show you in the il viewer where if i expand this over here and let's move up you can see all the code related to null guard throwing when that property is null so you don't have to manually check it if it is not defined as allow nullable it will throw however like i said you can say allow null here and that's a null guard attribute so if i go ahead and i debug now this is where i can find it stepped over the code didn't throw any exceptions because now the way the assembly was weaved is the check is not there so implicitly any parameter cannot be null unless i specifically say that they can and this is one of the three modes that null guard has one is implicit which is what you're seeing over here the default um if i have nullable reference types then it works a bit differently and if i go ahead over here and i enable it and i say nullable reference types enable um and i go back here obviously i'm getting the warning that you cannot convert this null literal to a non-allowable reference type but that's just a warning and you can end up with notes because this is just a compilation check it isn't a runtime check in reality so this code um if i didn't have folding here let me just quickly turn it off um this code over here let me just build it and run it i mean it works it didn't throw anything i still pushed null in something that isn't marked as nullable it will still accept it as a thing so i want to go back and uncomment this and add 40 back in and null god and what i want to do now is mark this as nullable so if this is nullable which realistically in this example it shouldn't be but for the sake of argument if it is nullable then i can pass null down and 4d won't do anything it will accept it however if it is not nullable and i pass null down it will now throw an exception as you can see so in this case i didn't have to say allow null my allow none is if i have the question mark and make this nullable and the project knows it automatically and marks it a certain way but if i don't have it then the check will be inserted so you have implicit mode a nullable reference type mode there's also a third one called explicit and that is driven by let me just go ahead and disable that again that is driven by a very popular nougat package that jetbrains the creators of ryder created and that is called the jetbrains.annotations and they had this package for years because they were using it in resolver in their own code and what this allows you to do is say not null coming from that package and if i do that then explicitly 4d will detect that and add the not null check here so this mode is enabled if you add that nuget package and you have not null anywhere in the project and this does not stop there by the way my personal preference because i am using nullable reference types is to just use the nullable reference types approach and not have to use the attributes but if you're in a previous version and you're not using nullable reference types this can be really useful actually and if you have a method and let's say that this returns a public string i don't know test and that string is null because strings are nullable objects if this was nullable and i go to the il viewer after i build then as you can see not much there just a regular null return here but if this was not nullable and i build it again then you can see that null god even on the return value if it is null it will throw an exception because this cannot be null by the intention of the developer so it will actually throw an appropriate exception to handle that and i think in this case it is an invalid operation exception not an argument null exception so it's a very well thought out library i highly recommend you go and give it a star and support the library and the creator i should point out at this point that this library and the whole for the project has a special license where you have to contribute to the project if you want to use it and you can learn more about that over here so there's a dedicated page that explains how you can become a patreon on open collective like all these people and support the project and this is part of the license is an faq you might want to take a look and even though this is an honesty system without any legal enforcement or any code checks to check if you actually paid or whatever if you want these people to keep working on projects cool like this i highly recommend you support them well that's all i have for you first video thank you very much for watching special thanks my patreons for making videos possible if you want to support us all you can find the link in the description down below leave a like if you like this video subscribe more than like listening the bell as well and i'll see you in the next video keep coding
Info
Channel: Nick Chapsas
Views: 50,499
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, dotnet, .net, .net 7, dotnet 7, Fody, assembly weaving, .net fody, nullguard, null guard, bang bang operator, null check c#, argumentnullexception, argumentnullexception c#
Id: rBzZd_ji7H4
Channel Id: undefined
Length: 9min 14sec (554 seconds)
Published: Thu Apr 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.