File Scoped Namespaces in C# 10 and .NET 6 In 10 Minutes or Less

Поделиться
HTML-код
  • Опубликовано: 5 ноя 2024

Комментарии • 111

  • @davecarroll
    @davecarroll 3 года назад +31

    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.

  • @PaulMcKillop
    @PaulMcKillop 3 года назад +19

    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.

  • @mioszkaczmarek6746
    @mioszkaczmarek6746 3 года назад +8

    You're my C# mentor. Stackoverflow is on the 2nd place :D Thank you for your work!

  • @bootsector13
    @bootsector13 3 года назад +1

    I'm glad you are making short videos now tim, thank you for the knowledge you shared.

  • @bigchunk1
    @bigchunk1 Год назад

    These sorts of videos work well with the youtube algorithm as I was able to find the answer to my question very quickly.

  • @uppercat7268
    @uppercat7268 3 года назад +1

    On the seventh day god didn't rest, he created Tim. Bless you.

    • @IAmTimCorey
      @IAmTimCorey  3 года назад +2

      I’m not that old.

    • @uppercat7268
      @uppercat7268 3 года назад

      @@IAmTimCorey Regarding to your wisdom, you're kind of.... Hallelujah ;). Warm greetings von Germany.

  • @banditul8381
    @banditul8381 3 года назад +9

    I love the file scoped namespaces. Less nesting

  • @michaelschneider603
    @michaelschneider603 3 года назад +2

    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).

    • @NathanHedglin
      @NathanHedglin 3 года назад

      Agreed. OCaml and other ML languages have always done this, well the file is automatically a namespace. Just makes sense.

    • @IAmTimCorey
      @IAmTimCorey  3 года назад +1

      The nice thing is that we have the option, should any edge case come up.

  • @venky76v
    @venky76v 3 года назад +2

    Brilliant Tim, the way you articulate and explain all the concepts seem so simple 👏👏👏

  • @evangreavu9621
    @evangreavu9621 3 года назад

    C# 10 is packed with so many exciting new features! I love this language and it just keeps getting better.

  • @Ala.obeidat
    @Ala.obeidat 3 года назад

    I really like the 10-minutes or less series

  • @jpboy1962
    @jpboy1962 3 года назад

    Love the new Namespace feature sure, but I really love the the new 10 minute or less series. Thank you so much!!!

  • @HollandHiking
    @HollandHiking 3 года назад +2

    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.

    • @IAmTimCorey
      @IAmTimCorey  3 года назад

      I hadn't thought about that, but you are right.

  • @mrsajjad30
    @mrsajjad30 3 года назад +1

    This is definitely going to save a lot of spaces for me ( and for our future generations :)

  • @vivekkaushik9508
    @vivekkaushik9508 3 года назад

    Keep'em coming Tim. Loving these short byte size vids.

  • @gaurlglmistrz
    @gaurlglmistrz 2 года назад

    This is a super cool feature! Thanks, Tim :)

  • @programmer5855
    @programmer5855 3 года назад

    Nice! Certainly prefer that to the standard way, looks much cleaner.

  • @DaNoob_777
    @DaNoob_777 3 года назад +2

    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.

    • @IAmTimCorey
      @IAmTimCorey  3 года назад +3

      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".

  • @pranilism_official
    @pranilism_official 3 года назад +1

    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 🙏👍

  • @petrutarabuta5617
    @petrutarabuta5617 3 года назад

    Thanks! Great feature, will use it all the time.

    • @IAmTimCorey
      @IAmTimCorey  3 года назад +1

      I’m not sure the extra words adds value but it is up to you.

  • @bjmmedeiros13
    @bjmmedeiros13 3 года назад +3

    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

    • @metaltyphoon
      @metaltyphoon 3 года назад

      You can declare classes without specifying namespaces at all ! They just go to the global namespace

    • @IAmTimCorey
      @IAmTimCorey  3 года назад +1

      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.

  • @alexcosta2614
    @alexcosta2614 2 года назад

    A welcomed addition. I never found the need two have two namespaces in a single file

  • @henrikcarlsen1881
    @henrikcarlsen1881 Год назад +1

    It's a good feature.

  • @eltaf00
    @eltaf00 2 года назад

    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.

  • @tonyschoborg
    @tonyschoborg 3 года назад +1

    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.

    • @IAmTimCorey
      @IAmTimCorey  3 года назад +1

      That will probably come eventually.

  • @danarj5713
    @danarj5713 3 года назад

    thanks Tim

  • @mohameddabbour6961
    @mohameddabbour6961 3 года назад

    Hello tim, thanks for the great video i really like those video nuggets,
    can you make a video about elastic search?

    • @IAmTimCorey
      @IAmTimCorey  3 года назад

      I will add it to the list. Thanks for the suggestion.

  • @JasonTira
    @JasonTira 3 года назад

    I like the space savings. Has there been mention of this becoming the default, or will we need to change it every project?

    • @IAmTimCorey
      @IAmTimCorey  3 года назад +1

      Right now it is not the default unless you turn it on as default using the editorconfig (that is per solution or per project).

    • @Scurvycapn
      @Scurvycapn 3 года назад +2

      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.

  • @DaNoob_777
    @DaNoob_777 3 года назад

    Thanks!

  • @savaged
    @savaged 3 года назад

    Thanks

  • @DarrenTAnims
    @DarrenTAnims 3 года назад

    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.

    • @IAmTimCorey
      @IAmTimCorey  3 года назад +1

      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.

    • @DarrenTAnims
      @DarrenTAnims 3 года назад

      @@IAmTimCorey 100% true 🙂
      It's still an improvement.

  • @metaltyphoon
    @metaltyphoon 3 года назад +2

    This feature should have been done 10 versions ago

  • @andreaskarz
    @andreaskarz 3 года назад

    How cool is that, wow 🥰

  • @christina-xn5my
    @christina-xn5my 3 года назад

    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

    • @IAmTimCorey
      @IAmTimCorey  3 года назад +1

      I am using Visual Studio 2022, which comes out in a week.

    • @christina-xn5my
      @christina-xn5my 3 года назад

      @@IAmTimCorey Thank you so much

  • @amirdar
    @amirdar 3 года назад

    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?

    • @IAmTimCorey
      @IAmTimCorey  3 года назад

      Yes you can mix them in your project.

  •  3 года назад

    Loved it

  • @ivandrofly
    @ivandrofly 3 года назад

    Thank you sir :).

  • @vorontsovru270895
    @vorontsovru270895 3 года назад

    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.

    • @IAmTimCorey
      @IAmTimCorey  3 года назад

      Yep, that would be a nice improvement.

  • @BikramjeethGarevaul
    @BikramjeethGarevaul 3 года назад

    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.

    • @IAmTimCorey
      @IAmTimCorey  3 года назад +2

      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.

    • @BikramjeethGarevaul
      @BikramjeethGarevaul 3 года назад

      @@IAmTimCorey thanx Tim. Appreciate your prompt and informative reply.

  • @haroldemmers3678
    @haroldemmers3678 3 года назад

    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 :)

    • @IAmTimCorey
      @IAmTimCorey  3 года назад

      Right now those features aren't a part of the editorconfig but you could recommend those as suggestions.

  • @markjastrzebski2065
    @markjastrzebski2065 3 года назад

    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

    • @IAmTimCorey
      @IAmTimCorey  3 года назад

      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.

  • @burnsy96
    @burnsy96 3 года назад

    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??

    • @IAmTimCorey
      @IAmTimCorey  3 года назад +1

      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.

    • @burnsy96
      @burnsy96 3 года назад

      @@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?

  • @errrzarrr
    @errrzarrr 3 года назад

    Short videos are better than large videoa

    • @IAmTimCorey
      @IAmTimCorey  3 года назад

      Short videos don't work for everything, but when they do they are nice.

  • @sergeymigel4680
    @sergeymigel4680 Год назад

    красава!

  • @JohnYoung77
    @JohnYoung77 3 года назад +2

    Is it just me? I prefer all the boiler plate code, makes me feel fuzzy :) Some of the new items are great though!

    • @IAmTimCorey
      @IAmTimCorey  3 года назад +1

      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.

  • @RafeSmith4Life
    @RafeSmith4Life 3 года назад

    If I do this I can no longer justify my super duper wide monitor purchases 😉

    • @IAmTimCorey
      @IAmTimCorey  3 года назад

      Windows 11 does multi-screen placement really well. It’s still valid. 😃

  • @HFamilyDad
    @HFamilyDad 2 года назад

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      But it is - the scope is the file. That’s different from getting the name from the file.

    • @HFamilyDad
      @HFamilyDad 2 года назад

      @@IAmTimCorey Yes ok, my idea should be called Project Relative Folder Scoped Namespaces

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +1

      I would like that feature.

  • @benjaminshinar9509
    @benjaminshinar9509 3 года назад

    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.

    • @IAmTimCorey
      @IAmTimCorey  3 года назад

      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).

  • @TheAngelOfDeath01
    @TheAngelOfDeath01 3 года назад

    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. ;-)

    • @IAmTimCorey
      @IAmTimCorey  3 года назад

      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.

    • @TheAngelOfDeath01
      @TheAngelOfDeath01 3 года назад

      @@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. :-)

  • @MarkPearsonmrp
    @MarkPearsonmrp 2 года назад

    I think this is not right i like indention

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      But why? They don't add any useful data.

    • @MarkPearsonmrp
      @MarkPearsonmrp 2 года назад

      @@IAmTimCorey I think they , I like the brackets and class and namspaces call me old fashioned but I like the old way