I really like the 10-minutes or less series. Sometimes looking at a video longer than an hour can be daunting, as I feel I have to watch it all at once.
This serves the 99.9% case, so I'm thankful for finally getting it. In fact, I have a hard time to come up with any reasonable usecase for having multiple namespaces in a single file. I guess, the reason for using the scoped syntax in C# was simply that C++ has it this way. On the other hand, Java has a single package declaration at the beginning of a file that looks just like the new file-scoped namespaces in C# 10, so they could have done it this way right from the beginning (although a Java package is not the same as a C# namespace).
I like this one. It combines good with best practices and it also helps to avoid stupid mistakes by trying to declare variables in the namespace instead of in the class.
Thanks Tim! I love file scoped namespaces. Simpler, less code, easier to read. Question: When do you use multiple namespaces in the same file? I have not used this scenario before.
I do not believe I have ever used multiple namespaces in the same file. It is a code smell, to be sure. I stick to the "one class per file" and the folder structure to match my namespaces. That means I never encounter this "issue".
Hey i love your videos, you are doing great, learning alot from your videos, but not getting how to apply these practices in best way, so had a little suggestion, please make short video on best project setup and newly trending "Clean Architecture" and its terms like Aggregates, events, etc. it would be very helpful to learn these concepts and apply them in real projects. BTW love your content 🙏👍
I wish they had taken it to the next level and allowed "top level statements" for every class (besides Program), where you'd use a similar 'public class NewWay extends Base;' syntax, just like with namespaces
Who knows, they might add that. You couldn't imply the class name entirely, since you would still need to apply the base class and interfaces to the definition, but we might be able to put it at the top like the namespace declaration. Maybe that will come in .NET 7.
Hello everybody. When I added an .editorconfig file via right click on the solution -> Add -> New EditorConfig the newly generated file overrides some of the default configuration in VS. To generate an .editorconfig file from your existing configuration you can go to Tools -> Options... -> Text Editor -> C# -> Code Style. There is a button to "Generate .editorconfig file from settings". I hope this helps.
This is a great and useful feature, thanks for the video. Would it not be reasonable to do the same with the class block? Since best practice is one class per file, and would save another 4 spaces.
You can make it a default for new projects by going to Tools, Options, Text Editor, C#, Code Style, General, then under Code block preferences, you can change Namespace declarations to File scoped. I just did this, created a brand new solution/project, added a class, and had a file-scoped namespace right off the bat.
This is nice. But every line will still be indented by a further 4 spaces because your methods sit inside a class. And then the code itself is indented by 4 as it's inside a method declaration. So this doesn't save a lot. However, what I've always wanted is for the code to be saved without any additional spaces (minified, but without name changes) and then have your development environment display the code in the format that you like. This also removes any arguments about code formatting. You can format exactly how you like and it doesn't affect other users. The formatting should be defined in your dev environment.
The .editorconfig looks so nice in your VS ♥. What Version of VS displays it like that? I am on Microsoft Visual Studio Professional 2019 Version 16.11.5
great video Tim if I set the namespace declarations to "file scoped" does it only applies to new classes I'm adding ? in theory, can I enable it to "file scoped" and still add a class in the old way? or will this break the code?
It would be great if the namespace was automatically replaced at the compilation stage with the path to the file from the base directory of the project, if it is not specified in the class file.
Hi Tim, a very important question for upgrading to .Net 6. Hope you can answer it ::: I have some .Net Standard Dll projects and some .Net Framework 4.8 Dll projects and some Winform Exe projects. I want to convert these to .Net 6. Will .Net 6 contain ALL the Namespaces and classes and methods for the conversion and also will they be in the same tree of occurrence. Or do I have to find in .Net 6 each of the classes and methods I used in my .Net Standard Projects and my .Net Framework 4.8 and my Winform Exe projects.
No, some things have changed, especially between the .NET Framework and .NET 6. Convert all of your libraries over to .NET Standard 2.0 first, then upgrade your UI project. Then upgrade the .NET Standard projects to .NET 6.
This is a feature I like. Especially because of the .editorconfig --> I did not know of its existance :) Would it be possible to make those Global usings visible as a kind of config file too? Then I would like it because it is visible and not hidden. Same goes for aliases. But I realy wonder when you could use that in a clear and constructive manner. Cool video by the way. I like the shorts :)
Do you mean the default namespace that you set in the project property dialog? Because that's not the same thing. Otherwise, I'm not sure what you are referring to.
Wait I just realised if I started using tabs instead of spaces I can compress all my source code 4 times over by using a single character instead of 4? Why are spaces the standard??
No, it doesn't work that way. Code is meant to be read by humans. That's why we use indents. When the code is compiled, it is rewritten for the compiler. Part of that process is removing all unnecessary spaces. Another part is renaming variables to use short names. It also removes code comments entirely. So the issue here is about horizontal space, not file space.
@@IAmTimCorey I mean yes I am going off topic here but in terms of just storing source code and not worrying about the compiled binaries because I still store the source on my drive, you can read both a tab and 4 spaces but with spaces I can save a little storage space for source code projects?
Its named wrong as its not file scoped! Why don't they just get rid of the namespace declaration altogether and just use where it sits in the file structure? i.e. a File Scoped Namespace.
this seems like a weird thing to do. you lose clarity, lose the ability to have nesed namespaces, and all this for some indentation gains? looking at all these changes, it feels like c# is trying to become a less structured program, like it's trying to dress-up as python or javascript.
You don't lose anything. If you find you need for an edge case, you can do it the "old" way. As for nested namespaces, I've never found a situation where that is a good choice (not saying it never is, but it is rare). Mostly it is a code smell. I don't see how this change is a loss of clarity. The namespace name is at the top of the file, just like always. The only difference is that you don't have the whole section wrapped in curly braces and indented. In a 100-line code file, you will save up to 400 lines of horizontal space with this change without losing anything in 99% of the code files (according to scanned open source repos).
To me, frankly, this is about as trivial as it can be: block-based vs. file-based namespaces because, for simplicity and the sake organisation and best-practice (inherent to SRP; Single Responsibility Principle) you should only ever have one namespace per file anyhow. I suppose you could make the argument that this sort of solidifies SRP, BUT really, that to me just presents the problem of creating a generation of programmers that doesn't understand the WHY behind SRP, and that, frankly, is the most important. And reduce white space as a reason? Those two or three, maybe 5 white spaces aren't going to make all that great difference; it's certainly not going to show up on any optimization chart. And besides, spaces are good for readability too instead of crammed-together-code or, even worse, code on a single line or where brackets have been omitted. ;-)
This isn’t about the compiled app but about readability. We are removing four spaces per line. That’s a lot. And really, we are only losing the curly braces. That’s it. To me, that seems like a pretty big deal.
@@IAmTimCorey A friend of mine said the same about his reasoning for cutting away brackets on IF ELSE statements, etc. To me, it hurts the readability more than it cleans things up because you take away the code that is a direct part of the code structure itself. Like you showed it yourself, you leave out things like the Data-folder and go directly to DataAccess. My concern is that it adds more reading complexity because you have to constantly sit and translate the code you read instead of that your code tells you the story. :-)
I really like the 10-minutes or less series. Sometimes looking at a video longer than an hour can be daunting, as I feel I have to watch it all at once.
I'm glad.
Tim, these short, topic-focused videos are a brilliant new tactic on the channel. I hope they get the traction they deserve. Thanks for doing them.
Thank you.
You're my C# mentor. Stackoverflow is on the 2nd place :D Thank you for your work!
You are welcome.
I'm glad you are making short videos now tim, thank you for the knowledge you shared.
You are welcome.
These sorts of videos work well with the youtube algorithm as I was able to find the answer to my question very quickly.
Great!
On the seventh day god didn't rest, he created Tim. Bless you.
I’m not that old.
@@IAmTimCorey Regarding to your wisdom, you're kind of.... Hallelujah ;). Warm greetings von Germany.
I love the file scoped namespaces. Less nesting
I agree.
This serves the 99.9% case, so I'm thankful for finally getting it. In fact, I have a hard time to come up with any reasonable usecase for having multiple namespaces in a single file. I guess, the reason for using the scoped syntax in C# was simply that C++ has it this way. On the other hand, Java has a single package declaration at the beginning of a file that looks just like the new file-scoped namespaces in C# 10, so they could have done it this way right from the beginning (although a Java package is not the same as a C# namespace).
Agreed. OCaml and other ML languages have always done this, well the file is automatically a namespace. Just makes sense.
The nice thing is that we have the option, should any edge case come up.
Brilliant Tim, the way you articulate and explain all the concepts seem so simple 👏👏👏
Thank you!
C# 10 is packed with so many exciting new features! I love this language and it just keeps getting better.
I agree.
I really like the 10-minutes or less series
I'm glad.
Love the new Namespace feature sure, but I really love the the new 10 minute or less series. Thank you so much!!!
Great!
I like this one. It combines good with best practices and it also helps to avoid stupid mistakes by trying to declare variables in the namespace instead of in the class.
I hadn't thought about that, but you are right.
This is definitely going to save a lot of spaces for me ( and for our future generations :)
Yep.
Keep'em coming Tim. Loving these short byte size vids.
Thanks!
This is a super cool feature! Thanks, Tim :)
You are welcome.
Nice! Certainly prefer that to the standard way, looks much cleaner.
I agree!
Thanks Tim! I love file scoped namespaces. Simpler, less code, easier to read.
Question: When do you use multiple namespaces in the same file? I have not used this scenario before.
I do not believe I have ever used multiple namespaces in the same file. It is a code smell, to be sure. I stick to the "one class per file" and the folder structure to match my namespaces. That means I never encounter this "issue".
Hey i love your videos, you are doing great, learning alot from your videos, but not getting how to apply these practices in best way, so had a little suggestion, please make short video on best project setup and newly trending "Clean Architecture" and its terms like Aggregates, events, etc. it would be very helpful to learn these concepts and apply them in real projects. BTW love your content 🙏👍
great idea
Thanks! Great feature, will use it all the time.
I’m not sure the extra words adds value but it is up to you.
I wish they had taken it to the next level and allowed "top level statements" for every class (besides Program), where you'd use a similar 'public class NewWay extends Base;' syntax, just like with namespaces
You can declare classes without specifying namespaces at all ! They just go to the global namespace
Who knows, they might add that. You couldn't imply the class name entirely, since you would still need to apply the base class and interfaces to the definition, but we might be able to put it at the top like the namespace declaration. Maybe that will come in .NET 7.
A welcomed addition. I never found the need two have two namespaces in a single file
Agreed.
It's a good feature.
I'm glad you will enjoy it.
Hello everybody. When I added an .editorconfig file via right click on the solution -> Add -> New EditorConfig the newly generated file overrides some of the default configuration in VS. To generate an .editorconfig file from your existing configuration you can go to Tools -> Options... -> Text Editor -> C# -> Code Style. There is a button to "Generate .editorconfig file from settings". I hope this helps.
This is a great and useful feature, thanks for the video. Would it not be reasonable to do the same with the class block? Since best practice is one class per file, and would save another 4 spaces.
That will probably come eventually.
thanks Tim
You are welcome.
Hello tim, thanks for the great video i really like those video nuggets,
can you make a video about elastic search?
I will add it to the list. Thanks for the suggestion.
I like the space savings. Has there been mention of this becoming the default, or will we need to change it every project?
Right now it is not the default unless you turn it on as default using the editorconfig (that is per solution or per project).
You can make it a default for new projects by going to Tools, Options, Text Editor, C#, Code Style, General, then under Code block preferences, you can change Namespace declarations to File scoped. I just did this, created a brand new solution/project, added a class, and had a file-scoped namespace right off the bat.
Thanks!
Thank you!
Thanks
Thank you!
This is nice. But every line will still be indented by a further 4 spaces because your methods sit inside a class. And then the code itself is indented by 4 as it's inside a method declaration. So this doesn't save a lot.
However, what I've always wanted is for the code to be saved without any additional spaces (minified, but without name changes) and then have your development environment display the code in the format that you like. This also removes any arguments about code formatting. You can format exactly how you like and it doesn't affect other users. The formatting should be defined in your dev environment.
It saves four spaces on every line in the file. Just because it doesn’t save 8 or 12 doesn’t mean it doesn’t save anything.
@@IAmTimCorey 100% true 🙂
It's still an improvement.
This feature should have been done 10 versions ago
Better late than never.
How cool is that, wow 🥰
Yep.
The .editorconfig looks so nice in your VS ♥. What Version of VS displays it like that? I am on Microsoft Visual Studio Professional 2019
Version 16.11.5
I am using Visual Studio 2022, which comes out in a week.
@@IAmTimCorey Thank you so much
great video Tim
if I set the namespace declarations to "file scoped" does it only applies to new classes I'm adding ?
in theory, can I enable it to "file scoped" and still add a class in the old way? or will this break the code?
Yes you can mix them in your project.
Loved it
Great!
Thank you sir :).
You are welcome.
It would be great if the namespace was automatically replaced at the compilation stage with the path to the file from the base directory of the project, if it is not specified in the class file.
Yep, that would be a nice improvement.
Hi Tim, a very important question for upgrading to .Net 6. Hope you can answer it ::: I have some .Net Standard Dll projects and some .Net Framework 4.8 Dll projects and some Winform Exe projects. I want to convert these to .Net 6. Will .Net 6 contain ALL the Namespaces and classes and methods for the conversion and also will they be in the same tree of occurrence. Or do I have to find in .Net 6 each of the classes and methods I used in my .Net Standard Projects and my .Net Framework 4.8 and my Winform Exe projects.
No, some things have changed, especially between the .NET Framework and .NET 6. Convert all of your libraries over to .NET Standard 2.0 first, then upgrade your UI project. Then upgrade the .NET Standard projects to .NET 6.
@@IAmTimCorey thanx Tim. Appreciate your prompt and informative reply.
This is a feature I like.
Especially because of the .editorconfig --> I did not know of its existance :)
Would it be possible to make those Global usings visible as a kind of config file too?
Then I would like it because it is visible and not hidden.
Same goes for aliases. But I realy wonder when you could use that in a clear and constructive manner.
Cool video by the way.
I like the shorts :)
Right now those features aren't a part of the editorconfig but you could recommend those as suggestions.
Better name would be better new old way. There was a base namespace in older versions of visual studio that would do the same thing
Do you mean the default namespace that you set in the project property dialog? Because that's not the same thing. Otherwise, I'm not sure what you are referring to.
Wait I just realised if I started using tabs instead of spaces I can compress all my source code 4 times over by using a single character instead of 4?
Why are spaces the standard??
No, it doesn't work that way. Code is meant to be read by humans. That's why we use indents. When the code is compiled, it is rewritten for the compiler. Part of that process is removing all unnecessary spaces. Another part is renaming variables to use short names. It also removes code comments entirely. So the issue here is about horizontal space, not file space.
@@IAmTimCorey I mean yes I am going off topic here but in terms of just storing source code and not worrying about the compiled binaries because I still store the source on my drive, you can read both a tab and 4 spaces but with spaces I can save a little storage space for source code projects?
Short videos are better than large videoa
Short videos don't work for everything, but when they do they are nice.
красава!
Thanks!
Is it just me? I prefer all the boiler plate code, makes me feel fuzzy :) Some of the new items are great though!
It isn't just you. The nice thing is that we have options now, so that both groups can have what works best for them.
If I do this I can no longer justify my super duper wide monitor purchases 😉
Windows 11 does multi-screen placement really well. It’s still valid. 😃
Its named wrong as its not file scoped! Why don't they just get rid of the namespace declaration altogether and just use where it sits in the file structure? i.e. a File Scoped Namespace.
But it is - the scope is the file. That’s different from getting the name from the file.
@@IAmTimCorey Yes ok, my idea should be called Project Relative Folder Scoped Namespaces
I would like that feature.
this seems like a weird thing to do. you lose clarity, lose the ability to have nesed namespaces, and all this for some indentation gains?
looking at all these changes, it feels like c# is trying to become a less structured program, like it's trying to dress-up as python or javascript.
You don't lose anything. If you find you need for an edge case, you can do it the "old" way. As for nested namespaces, I've never found a situation where that is a good choice (not saying it never is, but it is rare). Mostly it is a code smell. I don't see how this change is a loss of clarity. The namespace name is at the top of the file, just like always. The only difference is that you don't have the whole section wrapped in curly braces and indented. In a 100-line code file, you will save up to 400 lines of horizontal space with this change without losing anything in 99% of the code files (according to scanned open source repos).
To me, frankly, this is about as trivial as it can be: block-based vs. file-based namespaces because, for simplicity and the sake organisation and best-practice (inherent to SRP; Single Responsibility Principle) you should only ever have one namespace per file anyhow.
I suppose you could make the argument that this sort of solidifies SRP, BUT really, that to me just presents the problem of creating a generation of programmers that doesn't understand the WHY behind SRP, and that, frankly, is the most important.
And reduce white space as a reason? Those two or three, maybe 5 white spaces aren't going to make all that great difference; it's certainly not going to show up on any optimization chart. And besides, spaces are good for readability too instead of crammed-together-code or, even worse, code on a single line or where brackets have been omitted. ;-)
This isn’t about the compiled app but about readability. We are removing four spaces per line. That’s a lot. And really, we are only losing the curly braces. That’s it. To me, that seems like a pretty big deal.
@@IAmTimCorey A friend of mine said the same about his reasoning for cutting away brackets on IF ELSE statements, etc. To me, it hurts the readability more than it cleans things up because you take away the code that is a direct part of the code structure itself. Like you showed it yourself, you leave out things like the Data-folder and go directly to DataAccess.
My concern is that it adds more reading complexity because you have to constantly sit and translate the code you read instead of that your code tells you the story. :-)
I think this is not right i like indention
But why? They don't add any useful data.
@@IAmTimCorey I think they , I like the brackets and class and namspaces call me old fashioned but I like the old way