C# Hard Truths: Program.cs was a Lie, Startup.cs is a Waste of Space, and more...

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
program.cs was a lie up until.net 6. startup.cs was a waste of space Microsoft totally forgot about dry for years those are bold statements but I can back them up recently I've heard from a number of seasoned developers that they dislike the simplification in c-sharp in.net 6 and Beyond the thought is that top level statements and other major changes hid too much and that the original way was better in this video I'm going to show you why the new way of doing things in.net 6 and Beyond actually promotes better program practices let's Dive Right In and take a peek so here I have Visual Studio 2022 this is the 17.5 Edition right create new project and we're going to first start with a console application and I'm going to choose let's call this the old console and we'll call this the comparison app and we're going to choose.net 5 which yes that's out of support but that's pre.net6 changes now he could have chosen.net core 3.1 or we could have even gone back to Net Framework and seeing the exact same thing but Net 5 will work for us we're going to hit create and look at the old way of doing things this is the the console application in what many feel is the the right way of doing things which is to have your using directive you have your name space your internal class program your static void Main and then your code and this is where we start new developers or at least I do I encourage people when you're first learning c-sharp to learn in a console application because of its simplicity so this is the place to start but in dot net 6 and Beyond let's add a new project here a console app we'll call this new console and we're going to choose.net 7 because the latest version you'll hit create and this is what it looks like so there's a lot less here in fact there's just one line of code we have do have one comment by the way that new comment is talking about the new console template it has more information about it and why it is the way it is so we even documented hey there's some changes and here's what they are but let's get rid of that and we can see that that's all we have for program.cs that's quite a difference from this so we've obviously hidden a bunch of stuff here right well no not really so let's talk about why this is actually a very good change and why this is a lie so let's start off with the using directors using system okay yes the the system over here the using system directive over here is missing because it's an implicit using yes that is hidden and they they aren't shut up and yeah that's not the best you can turn those off if you want but at the same time let's talk about what it's actually doing it's not bringing in libraries that's never been a case so you can't compare it to like a C plus plus where you have the actual bringing in of libraries at the top that's not what we're doing we're just shortening our our namespace usage so instead of saying system.console.writeline we just say console.writeline Okay so yeah we're not showing that and we're hiding that I say we I'm using we as the you know Microsoft but they're hiding that and is that a bad thing I would argue no because are you ever going to create a class called console you shouldn't because you shouldn't be stepping on the same uh name spaces and Method namespaces classes and methods that Microsoft uses so you probably won't be doing that and in practically if not every class that you create in your original way of doing things you would have using system because it's a pretty fundamental Library it's built right into the core of.net in fact you don't have to use a nougat package to bring in system because that's just part of dot net core even though.net core has really shrunk down what is in just the core so it's already a case where you know there's kind of baked in it's part of.net so I guess you could argue well it'd be nice to see that and that's fine and you can show that but I really think it's baked right in and and so it's not so you'd necessarily need to know about and if you Mouse over anything that uses it it'll tell you well it actually comes from system so there's not a lot hidden there let's go now to this namespace old console here we really run into um the lie and that is that we have a namespace called old console cool that's based upon the project name we do not have one over here in fact if you try to reference something in the new console namespace you'd have to add a using directive well isn't that a good thing to have the namespace already no it's not here's why we had to dial all the way down here to this method this is the the central point of the program.cs is the main method and this is a special method it's one that when you execute an application it runs this method now how does the application know to run the main method well it just does it's part of how it's built in can you change that no not really so do you ever change that no never so isn't that just magic well yeah yes it is but but everybody knows you just run main okay so that's already magic the fact that this method gets called automatically when you run your application all right over here it just automatically runs program.cs so we we have the equivalency it's just that over here it's a method over here it's a file that's the big difference all right well but this is a method which you know when you first start learning C sharp you should learn about methods and namespaces and classes correct not in that order no not those things first in fact the first thing I'd encourage you'll learn about variables and yeah you're going to have to call console.writeland which is a method and kind of be like hey it's magic for a minute but we'll get to understand what this is doing but that's not a big deal as compared to why we didn't know what this or this or this right but let's just say you know you know what we need to know this right away okay so static void mate this is a method all right so we need to know what methods well what does a method do well a method is like console right line where we we can call something over and over again call a bit of logic instead of repeating ourselves so dry don't repeat yourself and so we call this over and over because the fact that it allows us to not repeat ourselves and that's why we create a method out of it cool so why is it we never call Maine because why would you create a method if you can never call it because that's really what it is static void me is a special method that we can never call because if we do we get into recursion now yes there are technically ways you can technically Force static void main to be callable again don't ever do that don't the the way our applications run is that the application starts on this Curry race it ends on this curly brace that's how all of our applications work that's how all.net applications work they execute the main method and they run until the end of that main method so we have a method that's different than every other method and we're saying it's important to have it as a method because that teaches people about methods but it teaches them all the wrong things because this is a method we never call again so encapsulates Logic for no other purpose than to encapsulate logic it doesn't actually allow us to not repeat ourselves it just creates additional noise so Microsoft said hey you know what we're not actually calling this so let's get rid of it because it's the same this magic calling main is the same as magic calling a file and then you look at the program or the internal class program and you say well why do we have a class that's main goal is to house the main method that we never call well that Pro that class serves no purpose in fact we never instantiate the internal class program and you can you could argue well I put stuff in there all the time but should you maybe it's not the best place to put that because that's all about the starting of your application now you're mixing multiple things in one class you're violating SRP this um the s in solid and really there's not a whole lot of value doing that compared to create a new file for those classes and methods so putting other methods in here not a great idea and not really of value so we have a class that's only ever called well zero times actually but one time because only referenced one time you call the main method inside of it and that's just because it's called magically so we have a class that's not really necessary so Microsoft said hey get rid of it and we wrap always in a namespace well why do you have a namespace on code what's the purpose of a namespace it's to be able to reference that code so the only reason we have a namespace old console is so we can say old console.program.main well but if we're not calling Main why do we have a namespace for it so now if we look at what we've done we've really essentially eliminated the need for this eliminate the need for this eliminate the need for most of this we'll get back to this which there is a need for that and we've basically eliminated the need for this as well so now we're left with three sets of curly braces and console.writeline in other words we're left with this so when Microsoft looked at program.cs they said hey we're really lying to ourselves because all these things in here the namespace the class and the method aren't normal they're not things we do the normal way they serve no purpose the only purpose they serve is to pretend to blend in with the rest of our code saying hey I'm just like all the rest of your code when we know we have to know in our heads oh wait that's not like all the rest of my code because it's blending in and saying hey I'm a method inside of a class inside of a namespace well any other method inside of a class inside a namespace we can reference we can call but you can't do this one well how do you know that well you have to know so what we're saying is you have to know a rule that's not stated explicitly in order to know how to execute this application you have to know not to do something and you have to know that this gets automatically called in order to understand the foundation of a c-sharp application compare that to this program.cs class is different than all the other things in your application but this is where your application starts that actually has less magic knowledge you have to have in order to know how to execute yes there's still some magic knowledge there's magic knowledge of you have to know that program.cs is the class file is the file that has to get executed in order to run your application but that's pretty much it now I did say there's one piece over here that I didn't talk about and that's this right here this is the one piece of all of this that I disagree with Microsoft on this is hidden it's the one thing in all of this that's actually hidden that's string array arcs over here we can reference string array arcs so I could say console right line args at position zero and if there was an argument position zero it would print out but where did that come from well we have to know it's magically there okay so yes that's magic that's hidden a hidden um meth or hidden uh parameter well that's not great I wish it was actually visible somehow but you know my my thing is what if we just did you know string array args like this and you put a semicolon and I'm not sure but you know that's not what Microsoft chose to do they chose not to show it and yeah I'm not a huge fan of that but that's the only thing that's hidden and if we're look if we're trying to compare apples to apples what are the magic things we need to know about in this new way the magic things are the string array args and the fact that program.cs gets executed when the application starts over here we need to know that static void main gets executed when the application starts and we need to know not to call static void Main so it's kind of two for two but we've eliminated instead of 12 lines we have one line of code so we've eliminated quite a bit of code and even eliminated three indents of code before we start to get the right code so this way is really the reason why it's the old way is because you know what it was a lie it was it was not doing what it said it was doing now that still does leave the fact that you're all what your old applications still do this and maybe if you create a new uh console application your organization you say hey I want to create a.net selling project but I do want the old way in order to line up with all the rest of our applications well this is where I really love the solution here because Microsoft said we get that so if you create a brand new uh console application let's create console um uh new but old console how about this it's.net7 do not use top level statements there you go it's the same practically as the old way of doing things the only thing is instead of using system it doesn't have that because it has the implicit usings and you can even come in here and turn off the implicit usings like so and then when you go to build this it's going to say hey you got a problem console does not exist in the current context we need to add that using directive and now you're back to what it was originally so you can turn off all of these optional things that make your code cleaner and make it less of a lie you can turn them all off if you want to match with your old systems but the new way of doing things is much much cleaner and as a new developer when you come in this is much easier to learn with you can say hey you know what we're going to teach you about methods this is a method but until then just know that if you say console right line it will print out whatever you put in here cool now let's talk about variables and you can create a variable int I equals zero and we can say console right line what is I and we can say hey you know what we can change the value of I so I plus equal four what's that going to be well I's now going to print out four instead of zero and you can learn about those things and eventually you say hey you know what let's learn about methods you can create your own little method so you can say um string test and you can have your test method do something where you can say a return hello and then you can print in you can say you know string info equals test like that you can get the value out you can print info out so now you learn about about methods which actually technically this is a local method that's okay you're learning up methods and how to call this more than once and then you can learn about classes you can learn about namespaces and all the rest when you create a new file when you create a new class so it allows you to still learn but learn less magic because right now we have one thing that's magic well a couple we run program.cs you have to know that but besides that it's console.writeline so you can kind of go from there and start to learn oh test is kind of like console Right light it's a method and it does something and this is a method and it does something so this allows for a much easier transition up the curve towards learning C sharp so for a number of reasons I really like this new way of doing things I especially like the fact that it's super simple so you can not have to eliminate a bunch of stuff you don't need it's just that one line you can get rid of and put our code in it so that's kind of the first thing I want to talk about the next thing I want to talk about is the fact that startup.cs was a waste of space so let's do that let's jump over to a new product on our solution and we're gonna go here to asp.net core web application which is a Razer Pages application we'll call it old Razer pages and we'll say Net 5 which again we can dot net core 3.1 as well but dot Net 5 is fine and we'll hit create and then we're gonna let's close all of these and yes we'll save all of them and then we'll create a new one new project another Razer Pages application this is our new Razer pages and we're going to say dot net 7. and again we have this check box we have we're going to uncheck this time do not use top level statements we we do want to use top level statements let's just keep that unchecked if you've noticed that yes top level statements are good and they're less of a lie than it used to be so let's hit create so now we have our two versions of Razor Pages let's talk about startup.cs well first of all you can notice we have 10 using statements at the very top that's a waste of space especially since these are all common they're things where I use over and over and over again but let's not worry about that for now let's focus in on what program.cs or what startup.cs is well we have a Constructor where we bring in iconfiguration okay got that then we have our configure services this is where we configure dependency injection which does for the basic application does one thing of course as you create a real application that will expand and then we have the configure method which is never called because it's actually called internally because it's a it's a class that Microsoft created but this is where we set up the development environment or the production environment using hsts and HTTP redirection and routing and authorization and mapping our endpoints okay this is where you do all this work and we're doing it all inside of the startup class inside of our namespace and then inside of a method inside of that class so that's the startup.cs let's look at program.cs where again we have eight lines of using statements that are pretty much repeated all over and then we have our namespace our class our static void main which calls one line okay remember static void main this runs until this curly brace which this run right here is going to keep it open until the application is given the closed Commander the stop command which comes from your web server so this will run pretty much forever which means we won't get to this normally um until the web server is shut down but our static void main has one method well okay we we call methods because we want to encapsulate some logic that we're going to call over and over right that's the thought but one reference create host Builder yeah it's called right here we create this method which is actually you know it's a simplified method but we create this method that only it's called here and this method calls the default Builder okay that's reusing a method over and over cool and that says configure host defaults where it says well the only thing we're going to configure is use startup okay so what this does it goes over the startup class and it uses this class to start up so really what we've done here in all of this is we have used create the defaults and then we've said run the stuff in startup and then build that and run that's the four things we've done on this page and then we just do the rest in startup so what's the point of this method why can't we just plop this right inside here and right there because we're not actually calling and I won't do that but we're not actually calling create host Builder anywhere else we could just put this in this spot and it would work just fine and why do we have another class that just is called once to do this work it doesn't seem like it's a logical reason for that instead we have this class that's that doesn't get used very much in fact it gets used once for just this okay so we have all this stuff or in the new way of doing things we put it all in program.cs there's we create the defaults for our Builder there's where we add our services there's we call the build method so okay remember we have a build method here we have in startup.cs we had the ad raiser Pages that's what we've done so far in these two lines and then we also had the create create default Builder well that's that's right here so these three lines take care of this they take care of this and they take care of all of this and then we have the configuration here that is hey if you're in development or if you're not in development do this and your https redirection your routing your mapping and all the rest that takes care of all of this method right here which means we've done all the stuff in startup which means we've done all the stuff here which means we've done all the stuff here which we've done all the stuff here which we replaced 26 lines here and 56 lines here for its 82 lines of code we replace it all with our 25 lines that do the exact same thing without going back and forth back and forth back and forth to configure everything so why wouldn't we do this well there is no reason this is the way to create a simplified system that does not leave anything out now why would Microsoft ever created the original system with startup and program well the the reason why is because this can grow you could configure more things in here change the defaults quite a bit and also you have something bigger here and startup is broken out so that you know when you have the ad server Computer Services that's configuring dependency injection and that's you know in one spot so it's easier to see oh this is the pansy injection here's all the stuff for that and so it kind of encapsulates things a little better because it knows well yeah for the demo and for the the starting you only have one line here but the reality is you'll probably have lots of lines to configure your dependency injection so you probably want to put that behind a method for cutting down amount of code you have to read in one spot that's all repetitive and allowing the um the startups do one thing well or the configure do one thing well or or whatever but the reality is that applications don't start there they end there so we have a simple application right now that fully runs and if we're a create a small application we don't really change anything at all this just works it's only if we get into a more complex application where we start to see oh instead of one line here we have 20. well now there's a lot more going on in this file I might want to extract that out into a separate extension method and make it a little cleaner kind of like this is this is actually an extension method that sets up more than just one service it sets up lots of services into our dependency injection you can create your own like that well but then you have a choice to do that you can grow into the application you need so what we were doing originally was setting up for a more complex environments that we didn't always need and putting a lot of extra stuff in here we also didn't really need we need to have two places for this logic when it's all it's all stuff that we do to start up our application which whereas our application start program.cs that's the starting point of our application so we combined both these things put it into program.cs and simplified everything then again like I said if your application grows you can grow your logic with it but there's no need to have a complex setup when you might not need a complex solution we add complexity to our applications in order to reduce bigger complexity so for example we add design patterns in order to reduce complexity but they're a complex piece of their own so they add some complexity but that ad complexity is less than the complexity it reduces right so it's a trade-off we we add stuff to take stuff make stuff simpler well in this case we start with the simple and we only add some complexities like additional startup files some extension methods um more configuration when we need it and when we need it that's when we we grow that that's when we say hey we're going to change and make things uh simpler by adding extension methods or whatever so this is the great a great starting point and this is a much trimmed much more lean trimmed down solution for us to start with and it's prepared for us to grow but it's not bloated already okay so that's why I think that startup.cs was kind of a waste of space we didn't need to have 82 lines of code between this and program.cs to accomplish the very same thing we're doing in 25. so this old way of doing things yeah you can go back to if you want because it's still there it still runs it will still work we can upgrade this old Razer Pages to.net Salem just by go over here and changing this to a seven and it will work but um just because it does work doesn't mean it's the right way of going this simplified where is it there you go I must have moved it let's try that come on move over there we go so this simplified version here is the one that's the the better solution to make things cleaner and more understandable it's much more readable and you're not bouncing around to find different parts that are all part of just starting up your application so that's my thoughts on startup.cs now let's talk about one that we've been seeing for a while now but I've been pointing it out but just briefly and that is this that remember I said that Microsoft forgot about dry for years I didn't mean everywhere I meant in one specific area using directives in our using directives if we create a new uh new page here so we have our all Razer page go to index we've got index.cs.html we have seven using statements and two of them are used all the time for example this one right here all the time used all the time for any Razer page why because it's necessary to have things like the page model it comes right from as microsoft.asp.core.mvc.razerpages.pagemodel so that comes tricky from there and logging if you use logging we're going to have this every single time system and generic and Link and threaten.tasks and MVC these things are almost always used now right now they're not being used but we have a very generic page once you start using any of our normal features like list all of a sudden that pops up and Alyssa T and you know if we do something that list it's link and so many other things so these things we've repeated over and over and over again as we go from page to page and the first you know six to 12 lines of our code file is taken up with these using directives and so Microsoft Kappa two separate systems for handling using directives when it moved over to net six so if we look at the index page here we'll see it has two and if we look at the error page it has three and we could even say you know what hey maybe you don't need all those so what it does is it has two different systems first of all there is the implicit usings so implicit usings are ones that are hidden and if we go to the properties page we'll see implicit usings currently is enabled this is again coming back to the fact that all these features that Microsoft have added to make things better are actually removable you can take them out you can say no I don't want to I want the old way I want more lines of code I want more clear braces I want more indentation I want more obfuscation so you can do all of that by turning off these features you can turn off top level statements you can turn off implicit usings but implicit usings are where they kind of they create for us some defaults so if we look here and say show all files and we go to the obj directory and the bin directory and then in.net 7 we scroll down here and see global usings this is the implicit usings so it says Global using Global these are the implicit usings that are auto-generated for us that Microsoft says hey you don't need to have those in every file now it's not everything and it's different for Web projects versus desktop projects and so on but these things are you are available then to all of our class files in the entire application so you don't have to add a using system directive because it's already there in the implicit using now implicit does mean hidden it does mean implied and yeah I'm not a huge fan of that but at the end of the day it's not a huge deal because these are not the same thing as Imports this is a confusion a lot of people have when they first start out they've seen C plus plus the idea that at the top of a C plus plus file you bring in a library and then you can use that Library that's not what's happening with using directives we're not bringing in system we're not bringing in system.io or system.threading we're not doing that all we're doing is shortcutting how we call these things so when I say console.writeline it's actually system.console.writeline but because I have a using system directive I can say just console.writeline so that's all it's doing is we're just cutting off those those long name spaces and saying hey you can use the shortened version instead and we're doing that for so the basics that Microsoft uses quite a bit and notice of these only six are actually lit up right now but that's all that we're using but or will come on board as we start using this as a real application so that's the implicit usings but if you notice and let's we can close this out if you know us let's turn this off that in index.cs HTML we have these two entries and up here we have those same two entries plus a Diagnostics one and if we go down to privacy we have those same two entries and that's a repetition again we're repeating over and over again but not every class file has these just the the pages files have them however the only reason you don't add a using statement for using directive for every single um entry in every single namespace is because the fact you can have naming conflicts so if you had a a class called console perfectly valid if you had a a method called right line perfectly valid but if you have that in a namespace called I don't know new console.console.com that'll work but if you had a using for new console and a using for system well now you have a conflict which is it called is it called the the local console.writeliner is it called the one from system which comes from Microsoft and actually it calls the local one but you shouldn't step on toes like that you should know not to and yes there is some implicit knowledge there but adding these using directives is not the end of the world because of the fact that most of the time you don't have namespace conflicts if you do then yes you have to turn off those using directives so if you found that you know what isn't step on my toes I want to add some more things you could add the implicit usings I don't like that solution instead what you could do is you could right click on your project you can say add new item and say global usings dot CS or whatever name you want to give it get rid of all this actually and then you can say well I want to get I want to just get rid of that one actually let's get rid of both of these since they use them quite a bit where I cut those out and notice over here we're gonna get some a red on a page model we paste it in here and say global and global save that and now no more red here it still says microsoft.asp.core.mvc.razerpages.pagemodel we can still see where it comes from if we hit F12 we still can go to where is declared just like before but now we have the ability to not have to have that repetition but don't repeat yourselves this is the other part of Microsoft solution for using directives personally this is a personal preference but I personally wish they would have created a file like this in our project that was the equivalent of what's in the implicit usings and just made it visible and it said Global using system Global using whatever and not had implicit that way if you didn't like it you could delete the file and if you didn't know what was happening you could look at the file so it'd be a little more obvious what's going on but otherwise this allows us to do everything we want to in fact if you want to you can go back to your implicit usings turn them off and just use Global usings bring in those things that are from the implicit usings and make them explicit but either way we now have a way to not repeat ourselves when over and over and over again we're adding the same using directive and we don't really need to we don't really need to make the top of our our methods massive with all the using directives that we need and no this is not hiding any dependencies so if we take this away it's not you're hiding what you're dependent on that's not how dependencies work you really should have your dependencies brought in with your dependency injection just having a using statement at the top doesn't really tell you much it doesn't tell you what part of Razer pages is depending on what part of logging it's using doesn't tell you if you know what part of generics you're using or how you're using link and so on it just says well you're using that library or that namespace but besides that we have no clue so this is not about dependencies okay so that's the using statements the last one I want to cover in this video is another one that has kind of annoyed people I get why but if we go let's close all this down we go back to let's go to the new console and let's create a new class so a new way on this is the new class dialogue love it um we can say uh person model okay and yeah the the um the templates have not been updated in Visual Studio in a while I'm not a big fan of that it depends on what project type you're using because the templates are different per product type so the new like Blazer projects will have better templates than the um the console does but unless we just get rid of all this we can for a curly brace or a semicolon there to get rid of curly braces and now we guys have our public class person model all right and now we can say prop let's do a string first name cool and it says well actually no it's not cool because I've got a green squiggly under me why well because it says non-nullable property first name must contain non-null value when exiting the Constructor you say wait that's stupid why is it yelling about string I know string can be null what's the point well this is the new null ability checks in dot net 6 and Beyond as something that is actually pretty important to allow us to make sure that we protect our applications against crashes protected applications against things that are pretty easy to check for if we just check for them so what is it doing well this string can be null did you know that it's not really clear that can be null but yes it can be done that's again one of those things that you just have to know is that clear no but it is what it is so string can be known whereas if we created a um an INT ID it doesn't yell at us why because ID can't be no because ins have a default value of zero so it's always going to have a value in this integer and if you want to make it null you'd have to say Well that's nullable and now this could be null you could have a null value in here so you could say equals no and that would be valid code whereas here this is also valid code if you don't have nullability checks turned on but we do have nullability checks turned on if we go to the project and look nullable enabled so if we turn that off it would not warn us about this but it says hey you can't put a null into a non-nullable reference type so what's saying is hey you know what you said that this can't be null now it's matching the same pattern as this even though it could technically at runtime contain null and it would work but we're saying hey you know what you're kind of indicating that this shouldn't be null and yet you're putting a null value into it and that can cause unexpected issues and so what it's saying is unexpected issues are a problem right so let's make them expected let's be clear about it and you have to choose what am I going to do about this so when you have this right here you can say well oops let's do something cool off too you can say well I want this to be nullable well then just put your question mark at the end now it's nullable now you're telling the the system yes there could be a null value here why is that important well let's come over here to program.cs and let's come down here let's go up here and when I say uh well actually first using directive so using new console we're gonna say person model P equals new it's known as shortcut things that Microsoft create we don't have to use it but notice that why do I say new person model I'm repeating myself second is say equals new I still know what it is it's still a personal it's still declared right there but this is I think better than saying new person models that's repetition and it's probably better than saying VAR because now what's P oh we can go over here and figure out oh p is a person model which yeah you can do but reading down we read down um and we can quickly see oh person model p versus this way right now where it says vrp I have to discover oh it's a person model so I prefer to have this where I'm not repeating myself but I also have it in line much more readable person model P so with Personnel P I could say um let's do a int length equals P DOT first name dot length okay and notice the squiggly here it says first name maybe null here because it's a nullable string so it's young to say Hey you know this could be a problem in fact it would be a problem because yes it is null here I have it assigned a value now if I said no this isn't gluing null and maybe not even a sign initial value like let's go a Constructor and I could say first name equals Tim as a default right well then over here no warning no problem you can do that because it knows for a fact that it won't be null we don't have to do a null check here whereas when I am unsure about things where I'm uncertain and I said well I haven't declared but this should probably not you know and it's going to say well then I'm going to warn you over here because this should not be null you've not said it can contain null value but if I say well yes it can contain null value well now it communicates over here and says hey you've got a problem once it updates uh you got a problem because first name might be null here so it's telling you hey let's be clear about when you're going to use null and when you're not if you're clear about it then I can tell you when you're making a mistake so we've been clear about the fact that yes this is nullable well okay then this could be null now if I come up here and say p DOT first name equals well test is fine well then guess what no more warning because it knows oh there was a value assigned up here therefore null is not a problem anymore so we can do this without doing a null check first or we could also do this where we could say you know what let's right click and say snippet surround with an if statement and say if P DOT first name is not no now we're going to null check and it said hey no problem that works I can do that because I don't have to know if this is knowledge I know you've done a null check right here therefore the only way this code runs in here is if it's not null so it's telling us hey we're good you've done your check you've made sure that I'm going to be protected but if we don't do that it's going to warn us to make sure that we check these things because this right here now it's pretty obvious right now because we're all in line but this right here is something that crashes a lot of applications because it's just that one time you forget oh yeah that's right the first name could be no and then you crash your application so that's something to look out for that's something that is really a big value to your application now when you get an application that's already existing let's say you have oh a very moderate application only 10 to 20 000 lines of code very kind of small application and you turn on the null checks you're going to have a lot of warnings to deal with because we have never used the the question mark with string before because string is by default nullable just like person model is by default nullable so when we we have this we're gonna it's we turn it on it's going to have all these warnings we have to say make a decision is first name nullable if it's not you have to assign a value and Microsoft's giving us the tools so you can either say assign a Constructor or we can say that it's uh required like so that also takes care of it what required does it make sure that when you instantiate your class that it yells at you and says hey you need to set the first name okay so we would say you know first name which is required equals dim and now we're cool so they've given us the tools to update our code to to match and make sure that we're checking for these things in the right way and that the visual studio can help us but this nullability check is kind of a big deal now again if you have an application that's you know got a few lines of code in it and you turn on nullability then what you're going to do is maybe turn it on per file which you can do or slowly build up over time maybe turn it on for a little bit and then um and then start adding your checks and don't turn on the warnings yet just say hey I want to allow you know stuff like required in the question mark and so on and that way you can start modifying your application not go crazy but it will make your application better so the things that Microsoft has been doing have made our applications better they have made them more more clear as to what's going on they have taken out a lot of the the guesswork or the even the outright lies about what these things actually are and they've replaced them with things that are clear that are simpler and that are going to help us build better code from day one okay so there's a lot of changes in.net six and Beyond and even if they were the right changes keeping up with all those changes can be hard so if you're looking for more information on a specific change you can check out the 10 minute training series I got a playlist that's got a bunch of those individual changes with just a quick 10 minute or less video that covers them and if you want to go to more depth and have a more comprehensive education C sharp go to im10core.com and check out the C sharp Master course it's one of the courses it's the one course you'll need to gain a solid education in C sharp whether you're just starting out or you're trying to keep up and fill in some of those gaps it covers C sharp from the beginning and it includes training in the.net framework through.net 6 and Beyond so you have a well-rounded education that fits any work environment all right so that's my thoughts on this that's my thoughts on the changes and how really they're very good changes and they are meant to make things clearer simpler not for the most part not hide things you know this notwithstanding and the implicit using is notwithstanding but otherwise to make things clearer and more obvious all right thanks for watching and as always I am Tim Corey [Music] [Applause] thank you
Info
Channel: IAmTimCorey
Views: 50,528
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# course, C# training, C# tutorial, .net core, vs2022, .net 6, .net 7, top level statements, using directives, global usings, implicit usings, startup.cs, program.cs
Id: aSNqqZqYTk4
Channel Id: undefined
Length: 53min 56sec (3236 seconds)
Published: Mon Mar 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.