Sitemap
John klaumann

John klaumann

Just another software engineer passionate about coding and learning new things while multiplying information and having fun in the process.

The best and simplest C# 10 features to implement

5 min readAug 25, 2022

--

The big day arrived!! Your lead or manager put on the board a story called “Migration to net 6.0” and all applications must migrate ASAP (as soon as possible). You and 100% others on the internet went to check the migration requirements and found that along with net6.0 comes C# 10 with several cool features. You give your lead the idea when planning to use some features along with the migration and this implies refactors, spikes and research. Your lead simply responds, “We don’t have time”. Well if this is your case or if you have all the time in the world or if you want to do it at home for fun, here are some features of C# 10 that are simple to do, that do not impact the flow of your code, which drastically reduces the amount lines of code and make your application cleaner and easier to read:

1. Global Using Statements

Microsoft has introduced globally valid using statements for you to not having to insert these statements into each project file repeatedly. Globally available once declared with the keyword global.

Lets say you have several classes on your project that deal with the same using's every time (System… Microsoft…) or you have made a huge refactor and some using’s you forgot to remove.

With this new feature you can easily reduce those issues. Let’s take this simple program.cs that is using a few common statement’s that you use on daily bases:

Press enter or click to view image in full size

Let’s eliminate these usings!!! “Eliminate you say?” “Yes and no”.

We are going to create a new file on your project called ‘GlobalUsings.cs’ and put there using’s with the keyword global in front of them, like this:

Ok cool, now let’s go back to our Program.cs and see if something changes:

Press enter or click to view image in full size

WOW!!! The colour changed and we all know what that means, lets remove and see what happens:

Eureka!! Clean code, less statements, new feature implemented. Do that on your application migration to net6.0 and start to evangelize your teammates.

Just a reminder: The GlobalUsings.cs you have to create for each project and not for the entire solution(would be great tho).

2. FileScoped Namespaces

Press enter or click to view image in full size

If you made it to the second feature implementation you are not a sayan anymore, and you became Super Sayan and you want more!!!

The new feature is FileScoped namespaces and it changes the way you use namespaces and brackets on your file. The namespace is then terminated with a semicolon (;), the two brackets are no longer necessary, which also eliminates the first indentation. So will go from this:

To this:

Notice the difference? ‘namespace’ will now receive a comma and the brackets are gone!

Get John klaumann’s stories in your inbox

Join Medium for free to get updates from this writer.

“Ok Ok, pretty simple, reduce a lot of code, make the file cooler for the ladies or the boys, but John, how I'm going to that on my entire application?? I have over 9000 files!! My lead will kill me!!”

Glad you ask, what if I told you that you can change all your files using editorconfig and a command line?

Lets do it! Open your IDE or a text editor, here I'm using VsCode. Create a new file called ‘.editorconfig’ on the root of your repository, like this:

Press enter or click to view image in full size

Now insert on your editor config the code like in the picture: csharp_style_namespace_declarations=file_scoped:warning

Using csharp_style_namespace_declarations=file_scoped as warning will tell your IDE: “Hey if some c# class is not using the new file_scoped feature, please warn the user to change to the new version”.

Pretty cool huh? Let’s see if is true on the code:

Press enter or click to view image in full size

Yes yes yes!! Your Visual Studio is already telling you to convert the code to the new feature.

Now let’s make it more interesting, lets format automatically using command line. Moving back to our VsCode we are going to create a new terminal and provide the follow command line: ‘dotnet format style’.

This command will pick the rules on your editorconfig and format the entire code of your solution, like this:

Press enter or click to view image in full size

And after running the command see your files on Visual Studio:

Converted!!!

3. Check Strings for Null

It is now easier to do this with C# 10.

Everytime you want to throw a ArgumentNullException you will do something like this:

Press enter or click to view image in full size

But now you can simplify things like this:

Press enter or click to view image in full size

See? Easy peasy lemon squeezy!! You can implement all these new features of net6.0 and C#10 without any complexity, make your code cleaner, with a lot less code and you are up to date with new stuff coming from new frameworks.

I hope you learned something and had fun in the process, peace and see you soon :)

Dive Deeper

https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10

--

--

John klaumann
John klaumann

Just another software engineer passionate about coding and learning new things while multiplying information and having fun in the process.