In this article, I would like to share 3 new important features which are added in C#10. They are "Global using", "Constant interpolated strings" and "Lambda improvement". All of them can remove the unnecessary code and enhance the code readability. To see how this work, let go thought this article.
Global Using
"Global using" is one of the big movement in C#10. It can remove a lot of unnecessary and repeated code in project. Suppose you create a class called "People" in people.cs file, and you want to use "Newtonsoft" library to serialize the class to json string in program.cs file. Then you have to include the "using" statement in program.cs file before c#10.
Without "Using" statement, the program cannot be compiled.
It is not a problem when you want to use this serialize function in a single file. However, how about ten files? It indicates that you have to repeat the "Using Newton.Json" 10 times. When the project grows larger, this number would increase until you don't need this funcntion. In order to remove this unnecessary action. In C# 10, it add a feature called "Global Using". In this example, I create a file "GlobalUsing.cs" to demonstrate the effect of this feature.
The "global using xxx" keyword can let the xxx module can be used over the project. It means that you do not need to use the "using" statement in other files within the same project. Let's go back to program.cs file. Now, without the "using Newton.Json" statement, the program still can be compiled successfully.
In addition to this, some common modules are available by default in the project. Before C#9, you have to include the "using System.Collection.Generic" for List collection. But this namespace have already been included in the project setting.
That is because the "ImplicitUsings" is set to true by default.
Constant Interpolated Stings
Before C#10, there is an compile error if you want to use the interpolation to concatenate the constant string. Therefore, the only way to do the string concatenation for constant string is + sign. Suppose the developer define a Endpoint class to save the API endpoint
If the developer want to concatenate the Root constant string with "DownloadData" , +sign is the only way to do this before C#10.
But now, the developer can use the string interpolation to implement the same effect.
String interpolation produces the better memory usage consequence than + sign, because + sign create a new string to get the final result in memory.
Lambda Improvement
Beginning with C# 10, the lambda expression can express as natural type. It means that developer do not need to use Action<...> or Func<...> to declare the lambda expression again. Before C#10, the lambda expression must be declare as following.
Func<int, int ,int> add = (int a, int b) => a + b;
But now, developer can define the expression as follow
The declaration is more concise than before because the compiler can infer the add to be a Func<int, int,int>. As a result of that, this lambda can assign to type such as System.Object or System.Delegate.
Wrap-up
In this article, I demonstrate the 3 important new features in C#10. There are total 14 features added in C#10. But I think the rest of them are not often to use in the C# project. That's why I select the above features to talk about instead of other features.
As always, thank for your reading. For you better understanding, please go to my Github to download the sample code.
Please leave your email below.