C# Namespace & Using Deep Dive - Including .NET 6 Changes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
name spaces and usings in c sharp are taken for granted but sometimes you need to take a step back and really understand what is happening with something so you can truly master it in this video we're looking at how namespaces and using statements work how to take full advantage of them and how they're changing in.net 6. now this is the first video you've watched of mine my name is tim corey and it's my goal to make learning c-sharp easier i provide videos twice a week here on youtube to help you grow as a developer i also have a full set of training courses on i am tim cory i encourage you to check out all the resources i have to offer now in this video as with most my videos i'm going to create some source code if you'd like a copy of the source code you can use the link in the description to get that now let's go over to visual studio but what you may notice is this is not visual studio 2019 which is the current version of visual studio instead i'm using the preview version of visual studio 2022 in fact this is preview 4.1 right now and the reason for that is because we're going to do some work with net 6 in this video and net 6 only works in visual studio 2022 from the the ui perspective of doing the file new project you can actually get uh net six to work with your visual studio 2019 if you use the command line to create the um the projects but we're not gonna do that we're gonna use 2022 and we're to start with a net five project and move onto.net six project so we're gonna do um a new project here we'll do console application and we're gonna choose we're gonna call this our namespace demo and our namespace demo app and we'll do.net 5 for right now we're going to come back and do a net sixth version there'll be some differences but i want to show you the kind of what you're used to with um with c-sharp right now because there are some changes coming in.net six that will make some shifts in how name space they're treated but i want to go over first what namespaces are and how they work and then we'll get into why these changes are happening and what they really mean for our applications let's hit create and here is the standard uh console application we've got our our class program our static void main and we have our hello world uh write line so that's the standard application now let's talk first about namespaces we have this namespace right here which you're probably familiar with this is the it's called namespace demo why is it called that well because we're in the namespace demo project therefore we're going to give the name of the namespace the same as the project name now if i right click here and i say add new folder and let's call this um let's call this data and in here i'm going to have a class and let's call this my data access class now notice here that the namespace is namespace demo dot data so what it's done is it said okay class name first and then if you're in a folder put that as part of the namespace with a dot in between so namespace demo.data.dataaccess and we're kind of familiar with this as what we use traditionally in c-sharp we don't usually modify these things but that's how we talk to our our classes in different namespaces now if i want to access that let's say i want to create that class i'm going to say instead of saying data access because that won't work it says hey i can't find that i don't know what that is then what i need to do instead is say data dot data access and that works i can say let's call it data access equals new like so okay but i said data.net access why'd i do that well because we're already in the namespace demo we're already in this namespace but it's a sub portion of a namespace a sub namespace called data so namespace.data data namespacedemo.data.dataaccess therefore we're already in namespace demo therefore just data.dataaccess but the full name would be namespacedemo.dataaccess i'm sorry.data.dataaccess which that's a mouthful that's a lot and so what we have is we have ways to shorten this up and the way we do that is with using statements and using statements what they do is just say where i can collapse multiple things into one therefore we're gonna say hey you know let's let's not have to type all this out so let's create a using up here for namespacedemo.data and then we can just say data access because it says okay look in here as well that's what we have the using system for because technically this is system.console.writeline so you have the using system at the top okay so that's the standard way we do things and there's a reason for this so let's let's take this off for right now let's go back to saying uh data dot now that's that that works but let's create a couple more class or a couple more folders actually let's create a folder called um oh models and in here i want to create a class called person model and you know we can we can put stuff in here like that first inlet first name last name just have something in a person model right so we can come back over here and we can access a person model by saying either models dot person model like so or we can say is have a using up here before we do that though let's create another model in the data folder called person model and let's in this one have an id and a name okay sounds good so this is a different model but has the same name which yeah yeah be careful that you don't really want to have two things that name the same but have different values but it's possible to do and there are reasons to do this and it's okay to access this person model because if you look at model dot we'll see first name and we're going to see down here last name as well so it has both of those and if we were to say let's come down here and say data dot person model and let's create that and now if we look at person model this person model dot we have name and id we don't have a conflict all right so that's fine but what happens if we bring in our using statements so using namespace demo dot data and using namespace demo dot models let's bring both of those in and get rid of these additional pieces of information notice i have red squigglies now why do i have red squigglies well because the person model is an ambiguous reference between namespacedemo.data.personmodel and namespacedemo.models.personmodel it doesn't know which of these person model classes to choose from and the reason why is we brought both we collapsed both of these into the same name space we we're looking at all three places and not specifying which one is which so if we were to say data.model person model that's clearer and if we were to say models dot person model that's clear as well but now i i went the wrong direction um let's swap those around so so now these match okay so now this is i know which one you're talking about because you're saying you're referencing the namespace or at least part of the namespace therefore we're good i know which one is which and we can we can access that but if you try and collapse it all down and you have a name conflict that's when you have a problem the reason for these namespaces is to prevent these name conflicts so that we don't have to have a enormous list of all the method names that microsoft has ever created that we might want to use but we can't because they're now reserved the only way they're reserved is if we're bringing in the using statement for that particular area and then we might have a conflict which we can get around by just using the the namespace or more of the namespace in order to specify this is the one i'm talking about not that other one so namespaces provide some protection against naming conflicts they also provide for us structure so that when i say namespace demo dot data what do you think is in the data section well probably things about data now i made a little generic but what do you think is in the dot models section well probably things to do with models data models so this allows us to have some structure around our application so this way we can say i want to have you know all of my data stuff inside the data name space and it kind of groups things together now we already do this with folder structures the namespaces just usually usually match the the structure we create with our folders that doesn't always have to be the case though we can create our own namespaces if we want so for instance if i'm in this um data access class and it says namespacedemo.data i'm going i don't like that i want this to be dodd's generic data you can do that it's not common practice it's not great practice but it can be done and there can be reasons to do so but in general we leave the namespace to match the folder structure just like we have the class name match the file name so that we have some consistency between the file system and the code that's inside of it however there may be times when it's important or valuable to match the name space into something else besides just the folder structure so with this we can choose whatever we want we could say tim is cool dot generic data that is a valid namespace now if they come over here go did access it goes i can't find that we go oh no problem i'm not sure why this is still here um i can't find it not a problem we can do is we can say [Music] tim.is.cool.gov generic data data access which that's a mouthful but it is a an the way to reference this data access class so we can have our own namespace to be whatever we want them to be but we can also say you know what let's change the namespace to be something else for example what if i wanted to let's create a new class let's create a class called i'll put in data but really could be anywhere um better console better console okay this is going to be my better console that's going to work better than the console that microsoft gives so instead of having a console writeline we're going to have a public class or public public method there you go um yes sorry public string avoid there we go wow i'm blanking here public void um better right line like so and we'll make that public as well and now in here we're gonna do a console right line this is a better right line like so whatever you know this is just a demo okay but i have this better console but right now it's in the namespace demo.data i can change this namespace to a system system comes from microsoft okay but i've chosen to put it in the system directory anyways now when i say system dot i have console or i have better console you see how my method showed up am i sorry my class showed up because of the fact that i put it in the system namespace this allows us to merge namespaces even ones we don't control i don't control the system namespace that comes from microsoft but i can stick my own things in there now a word of warning here a word of caution just because you can doesn't mean you should all the time don't just decide that this means you're gonna put all this stuff in inside the microsoft's namespaces you could do that but note that microsoft's not keeping track of your code so that if they put new stuff in the system namespace it's not going to conflict with microsoft stuff but may conflict with your stuff if you put your stuff in the system as well so this could create breaking changes for you where it's not breaking change for anybody else because no one else is messing with that namespace so be careful of it but there are times when this might be a valuable thing to do to add something to a different namespace so there's some cool stuff you can do there where hey you you put your own stuff in a namespace that that's not even yours which means also if we come back over here i have a using system up here let's pretend i didn't have this well either of these so i don't have a using for namespacedemo.data for better console but i can still come down here and say better console dot or you know b equals new better console and then say b dot you know better right line like so i don't have to have a namespace because the fact that it's in this system namespace which is usually already included in our namespaces so there are benefits there as well where you can bring something in it's already usually there and kind of tag along with it if you've got stuff that you always want to be available without using the namespace so there's some things you can play around there with namespaces but let's address this one right here tim is cool dot or tim dot is dot cool dot generic data that's an awful big mouthful but sometimes maybe you can't just put a using up here for tim dot is dot cool generic data maybe you have that naming conflict between two different things you don't want to step on any toast so there is a way around that we could say using i'm going to say timothy and the reason i said timothy is because tim already exists as a namespace so timothy equals tim dot is dot cool dot generic data what i've done here is create an alias for a namespace called timothy now timothy cannot be the name of a namespace so that's why i couldn't use tim because tim is a namespace therefore it wouldn't know it's the namespace or is the alias so this way it has to be a unique name but with this i can say timothy data access so and that just works in this file but this allows me to alias a using statement with something that's shorter that's um clearer and allows for less typing and less horizontal code use so some cool stuff there with using statements as well now that's the the generic or general stuff that we're dealing with with namespaces namespaces are like i said they kind of sit in the background and just work and by default we keep the structure of the the project name is the root namespace and then we have the folder structure in here that we follow uh for sub items you know so person model and here is namespacedemo.data because it's in a data folder that's the general thing that we do with namespaces so let's talk now about net six right click on your solution and say add new project console application let's call this um let's call this dot net six console instead see it's clear which one it is and we're going to choose the next text preview now if you don't have visual studio 2022 installed um or the the.net 6 preview bits then this might not work in your machine but you can watch it on mine let's close everything else out but this and let's look at i'm gonna actually put some line breaks in here can come down a little more look at where the code come it this is the new program.cs for a console app now you may be freaking out and saying tim where is all the code because it's got one line of code here and yes you do and that's all it needs in fact if we were to set this as startup project and run this this would run just like any other console app hello world and yet we have just one line here so let's talk about what's where everything is so let's go back to our uh our program.cs from.net five and in here we had using system and namespace of namespace demo okay let's start there we're missing those over here there is no namespace of net 6 console and there is no using system and the reason why is because we have the idea of file based namespaces so by default what the system is doing is saying hey i know what namespace you're in you don't have to specify it because we normally follow the file structure therefore that's what i'm going to assume unless you tell me differently so why do we have these name spaces over here why do we say namespace demo when that's what we always use we always use the the project name as the root namespace and we always use the folder structure underneath as the the sub uh name spaces if we always do that then let's assume it and that's what we did here we assumed it which got rid of that namespace declaration and it got rid of note we indent one line here or one tab indent or spaces indent for the class because of it's inside the namespace well we don't have to do that here because we don't have to specify a namespace it's being it's implied so with that being implied we get rid of one level of code indents that we don't need and we get back a little bit more horizontal code space so that's one of the new things in net is this assumption so if i were to create a a new let's create a new directory or new uh folder let's call it data and in here we're going to create class and let's call it person model now we have all of this because the fact the class is this class template has not been updated so what we can do is we can say you know what get rid of all of this and get rid of that last credit brace and let's bring that back that still works and i can still say um prop int first name i'm sorry id and there's our name okay so there is our two properties that will work and if we reference the person model in our right here we can say instead just say in person model if we say person model it's going to assume that person model is in the same name space okay we have that reference right here so it says okay i'm going to assume it's the same namespace which it's not it's actually should be in the data namespace so um so we can do here is we say no we want this to be a different namespace we can say okay no problem using dot net six console dot data like so i'm sorry namespace namespace of.net console.data so this is another new feature of.net 6 where we say hey if you do need to specify a namespace no problem you go ahead and do that but just say what the namespace is at the top call it good and now this entire file is going to be inside of this namespace so now we don't have to specify what the namespace is notice that the person model isn't here yet so using dot net 6 console dot data like so now just you know there are a couple quirks this is still preview versions preview bits um the templates for the classes are still the old templates i assume they'll be updated soon with the new templates also um i do think that this should be by default uh this namespace because that's what the default normally would be but uh and that may come down the road but for right now you can specify it this way we're going to say namespace like that like so and you could specify something else so if you want to specify tim is cool dot data um you could do that as well now the other thing i note here is that you can't decide to put another namespace down here so i can't come down here and say namespace uh tim.is.cool that's not going to work because the source file can only contain one file scope namespace declaration it says now you you can't do that because how we know what's in this namespace and what's in this namespace we can say well the stuff underneath it no we're not we're not going there at least not for now so in c sharp 10 we can't do that one namespace per file and that declares for the entire file but with that we we probably should want to follow that direction anyway because the fact that just like we put one class per file we should probably have one namespace per file and if we wanted a file like more stuff in other namespaces we should probably create more class files or more um cs files so the other thing to note here is we come back over to program.cs notice there's no except for mine there's no using statements here and yet we have console.writeline and console is in the system namespace so where is that using statement because there is one it's just not here so where did that using statement go well if we right click on our project and say open folder in file explorer and then in here we can go to bin debug net60 and then inside of here i'm sorry not in bin obj obj debug net60 there we go that's the one we can see right down here we have net6console.globalusings.g.c.s if we drag that in to look at it what this shows us is global using global system so this is an auto-generated file and it will be different depending on if you're using it for a a desktop app like a console app or if you're using it for a web app like a asp.net core app or blazer app something like that or if using it for i believe a worker service is different as well so there are certain default usings in here where it says hey you know what um these are probably defaults that you want to use across everything therefore instead of specifying these seven things everywhere we'll go ahead and collapse this list into one and say that these come by default and that shows up in your obj folder and gets merged in so if you want to look and see oh this is what's coming in that's what's not there you go now that's that's the default but maybe you want to add your own like maybe i want to say hey you know what i want this using statement all the time everywhere well you can create a file it the convention is still kind of solidifying here but um if you right click and say add let's add a class file but it's really going to be called our our usings dot cs i'll put the root and i'm going to get rid of this whole thing right here here of everything and that's my global using file so i can say global using and then say dot net 6 console.data i hit save oops not open save then come back over here notices grayed out it's no longer necessary to have that because i can still access person model in the.net 6 console.data because of that global using you only have to have us one time so you create one folder or one file i'm sorry at the root called usings and put all of your global usings in that file and then they referenced everywhere but remember you don't want to put too much stuff in here it's not going to hurt your application it's not going to slow things down really what's going to do though is it could cause naming conflicts remember that namespaces give us that separation for avoiding conflicts in names so that if we have a person model and data and a person model and models if we had both of those using statements together as we saw that would cause a naming conflict so you don't want to go overboard with this but there's times where you say you know i'm always adding these using statements well just put them as a global statement they're accessible everywhere without the name space on the front so that makes our file a little easier so that's that's how the the using global usings work that's how the implicit global usings work where they're kind of hidden but they're there i do not point out if you go to the dotnet console cs proj file there is this implicit using that's enabled by default on new projects in net six if you upgrade a project from five to six it will not be enabled by default so that those global usings won't be turned on because they could be a breaking change again naming conflicts so you can come in here and add this implicit usings to a file for this.net six and say enable and that will turn those on if you don't like them go ahead and turn that off that's nothing you can do there's also another option so let's just say for whatever reason that you didn't like the fact that system was a global using you liked everything else about the global users just not that one particular using not a problem come to your cs proj file oops there we go uh you can also right click and say edit project file to get this in case uh just selecting it didn't work for you but outside the property group we're gonna say item group and in here we're gonna say using remove system and that will remove the system from the global using list now console is going to error out on us notice the red squigglies and it says hey it doesn't exist in the current context because you don't have that using you'd have to either add it back or come down here and say system dot and now it'll work so you can take that out if you want by modifying your cs proj file or you can put it back in you can also so let's um let's keep it out for now but let's just say that instead of having this global using here maybe i don't want that okay i'll comment that out but that causes a problem over here but you know i say i tell you what i don't want that file hanging out there but i'll come over here and i'm gonna add a using and instead of remove what i'll say is include and this is a just a text file like there's no intellisense here but let me copy and paste it paste that in there that's now included in the global usings list so now this is again available to us because it's in the global usings so that's now the implicit global using whereas if we were to type it out in our using file like this that's explicit where we are saying this is a global using okay now there's another thing you can do as well let's create um another class i'll create right in the root um i can't call the silly class because i'm going to say tim uh let's call it tim let's call it tim is cool tim is cool okay so tim is cool is the name space and is for the silly class but i don't want to reference i want to say i want to say with silly s equals new like so but how do i do that well i can do this a number of ways but maybe i want to say timothy dot silly use that alias and again i could come up here and say using timothy equals tim.is.cool like so that would work but what if i don't want to have that using statement here i want to have this alias available everywhere well i can come over here and i can say using include tim.is.cool and i can say alias timothy now this is not going to create a i start to use the namespace the alias of timothy but this is now available everywhere as well so i don't need this anymore and this is still available to me you can mouse over it's actually a namespace tim is cool because we have it in our global settings so some cool new stuff coming with net six this latest stop the stuff we're doing in this new project is all dotnet six related the big deal here is it's all about cutting down on the boiler plate the stuff we have to do over and over and over again the the stuff that is just kind of set up work that really can be assumed in most cases so again if we compare let's let's close everything out with this and compare it to program.cs notice first of all this this groove quite large we have 21 lines now these lines you know um we can we can exclude but that's only uh six eight lines so this took quite a few lines of code here so we have our using statements up here which we don't have to have those we have the namespace demo we don't have to have that because we can imply that based upon where it's at and we have the class defined well we've already lost that in in c sharp nine but we don't have to have that for our program or yeah our program class because the fact that we know that we have to have a starting place somewhere so if we call the the um the file name program.cs we can imply the fact that that's the program class therefore we're good to go without having to do anything more than that and so that cuts out three or i'm sorry two layers deep of of nesting and it cuts out one two three four five six lines of code that we really don't need so now we're down to just this i'm sorry seven lines we don't need that using statement either and we're down just to the static void main and since it's program.cs we can assume the top level code before you start calling methods the top level code that's going to be the program.cs itself that's the actual code for program.cs because usually when you're setting up a a program.cs and a static void main you're not putting other methods in that same class because it's a special class it's a special method and you're really trying to call out from that you're trying not to put other things in it well this kind of helps you along that way and says we're going to give you a way to do that if that's what you want to do you can still wrap it you can still put the the class name back in if you want but this way instead of having these two lines or this line we've now dropped 10 lines of code that we don't need that's just can be implied so now we get right down to the cody right and we are three indents in okay we've saved ourselves three indents in our code so that's kind of the point here at dot net 6 is trying to eliminate the stuff that we can imply and yeah at the same time if we say no that's not i i know that that's normally right but that's not what i want to do here then you can go ahead and specify things like the namespace still no problem go ahead and specify that and we're just not going to make you do a an indent for that so that's going to save you some space as well and you could still put the indents here if you wanted to if you want to have multiple namespaces in one file you can still do that the option option's still available to you but there's a way now to really shortcut this and cut out a lot of the fluff that really is just fluff it's really not necessary now that does mean there is more to learn in some ways um because there's some stuff that's behind the scenes some stuff that's that's still there it's just kind of hidden a little more and yes that is kind of a bummer but the the upside is that once you learn it then there is um you know less boilerplate code being written less in front of your eyes you have to look through i mean look at the difference okay these are essentially the same bits okay i have three lines here instead of um six lines of code but besides that the same same thing yet this is really simple and that is got a lot more to look at so that's kind of point here and with name spaces this is they're still there they're still functioning the way that we've always had them function it's just now they're kind of hidden in the background they're assumed instead of required to be explicitly in front of us so that we can get to our actual code instead of focusing on the setup and tear down of our code so that's kind of a deep dive in a name spaces how they work how they can be beneficial to us how we can modify them and tweak them using things like um our aliases and then how to get into the new stuff with namespaces in.net six how they can work with the um the shortening of things and and see how you know just how implied implicit namespace and how we can bring in namespaces um in our implicit using so that we can not have to have these or we can have them back in if we want to i can add using statements with aliases right in the the the cs proj file or in our usings file for global using there's a lot of cool new stuff with name spaces name spaces have finally gotten a little bit more love and a little bit more attention to kind of again reduce that boilerplate at the same time give us more features more possibilities not less so hopefully that explains namespaces if you have any questions leave them down in the comments below i also want to hear what your thoughts are on the um the new.net 6 stuff how they they shorten things and have global usings and have uh global aliases and implicit usings and all the rest of the cool stuff is going on i want to know what your thoughts are on those i know sometimes it can be you know one more new thing to learn but at the same time do you anticipate using these do you do you find that this might you know reduce the indents in your code or may make your code more readable what are your thoughts on this leave them down in the comments let me know i'll try to re at least read over them and maybe interact on a few of those all right thanks for watching as always i am tim cory [Music] [Applause] you
Info
Channel: IAmTimCorey
Views: 14,755
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, .net 6, .net 5, file based namespaces, global usings, global using c#, top level namespace, c sharp, c# 10, visual studio 2022, .net 6 new features, .net 6 features, visual studio 2022 preview 4
Id: 0f0K8vVJzkg
Channel Id: undefined
Length: 43min 30sec (2610 seconds)
Published: Mon Oct 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.