This post is part of a series on .NET 6 and C# 10 features. Use the following links to navigate to other articles in the series and build up your .NET 6/C# 10 knowledge! While the articles are seperated into .NET 6 and C# 10 changes, these days the lines are very blurred so don’t read too much into it.
This is probably going to be my final post on new features in C# 10 (Well, before I do a roundup of everything C# 10 and .NET 6 related). But it doesn’t mean this post is any less useful. Infact, this one hits a very special place in my heart.
For a little bit of a story. Back in 2006-ish, I wanted to learn a new programming language. I was a teenager all hyped up on computers and making various utilities, mostly revolving around MSN Messenger auto replies and the like. I had mastered Pascal to a certain degree, and had moved on to Delphi. There was this new thing called “.NET” and a language called C# – and since anything starting with a C was clearly amazing in the programming world, I went down that rabbit hole.
I convinced a family member to buy me a C# tutorial book *I think* from Microsoft, I can’t exactly remember. I do remember it having a “tool” on the front so I can only presume it was this one or another in the series : https://www.amazon.com/Microsoft%C2%AE-Visual-2005-Step-Developer/dp/0735621292. Eagerly, I opened the book and inserted the CD Rom that came with it. And I can still remember my heart sinking.
Your operating system is not compatible
For reasons unknown to me at the time, I was using Windows ME. Quite possibly the worst operating system known to man. I mean, we didn’t have have a lot of money. It was a 1GHZ, 256MB RAM machine, Windows ME was the best we could do at the time. And so.. I was stuck. The CD ROM wouldn’t work, so I couldn’t install Visual Studio (These were days before broadband/ADSL for me), and so I did what any kid would do. I just read the book instead and took notes that someday I hoped I could use when writing C# code. Literally, I couldn’t even write C# code on my PC, and instead I just wrote it on paper and “pretended” it would work first time and I was learning. Ugh.
However, the actual point of the story is this. The first chapter of the blimmin book had the driest introduction to namespaces you could imagine. I thought maybe we could ease into “integers vs strings” or a nice “if statement”, but nope, let’s talk about how namespaces work. I just remember it being sooo off putting. And 15 years later, if a new programmer asked me to teach them C#, I would probably not even mention namespaces in the first month.
So with that story done, let’s look at the actual feature….
Introducing File Scoped Namespaces
We can take a namespace scoped class like so :
But in C# 10, we can now remove the additional parenthesis and have the code look like so :
And that’s… kinda it. It’s done for no other reason than it removes an additional level of indenting that really isn’t needed in this day and age. It just presumes that whatever is inside that file (hence file scoped) is all within the same namespace. I can’t think of a time literally in 15 years that I have ever had more than 1 namespace in the same file. So this addition to C# really does make sense.
Visual Studio 2019 vs 2022
I just want to put a huge caveat when using this feature. For a couple of months now, I tried this feature out in every .NET 6 preview SDK release. And each time I couldn’t get it to work, but I kept seeing people talk about it.
As it turns out, for whatever reason, I could not get this feature to work in Visual Studio 2019 (And actually, Minimal APIs in .NET 6 had similar issues), but it worked first try in Visual Studio 2022. So if you are getting errors such as :
Then you probably need to try it inside Visual Studio 2022.