This week Microsoft released an exciting post going through the new shiny features in C# 10. As with every release there’s a lot of fluff that is aimed at extremely niche use-cases, but with C# 10, there’s a few noteworthy features that’ll really change the way we write C# — for the better.
In this blog I’m going to go through the features that excite me and hopefully explain where they sit in my arsenal.
Let’s crack on.
File-scoped namespace declaration
One of my biggest gripes with C# — coming from a Java background — is the sheer wastage in vertical spacing. It took me a long time to adapt to plonking curly braces on their own lines. I still don’t love it, but nothing’s perfect.
File-scoped namespace declaration helps to combat this wastage saving us both vertical and horizontal space, this is a big win for me!
Before:
namespace Messenger.Inbound
{
public class Message
{
}
}
After:
namespace Messenger.Inbound;
public class Message
{
}
That’s two braces we don’t need and one level of indentation less, marvellous! I imagine this wasn’t the initial solution to namespaces because the C# designers assumed consumers would use multiple namespaces in a single file, I’ve personally never seen anyone do it in the wild and can’t imagine a use-case for it.
Global using
You can now specify a global using statement. Let’s say that you’ve noticed every one of your files does some logging so you want to use Microsoft.Extensions.Logging.ILogger
everywhere. You could — in any file — add the code:
global using Microsoft.Extensions.Logging.ILogger;
Because these can be plonked anywhere your team will likely have to agree on where to put them, maybe creating a GlobalUsing.cs
file where you only allow global statements to be added:
global using System.Collections.Generic;
global using System.Linq;
global using Microsoft.Extensions.Logging.ILogger;
These can also be added in your .csproj
:
<ItemGroup>
<Import Include=" System.Collections.Generic" />
</ItemGroup>
But I don’t love this, I imagine a GlobalUsing
file would be quite useful in some scenarios and once again, save us on precious vertical spacing.
Constant interpolated strings
Exactly as the name states, you can now define const
strings using string interpolation. Provided the thing you’re interpolating is also a string constant, you’re good to go:
Before:
private const string Version = "v1";
private const string AddPost = Version + "/posts";
After:
private const string Version = "v1";
private const string AddPost = $"{Version}/posts";
Working with Azure Functions we’re constantly doing some form of string concatenation to state our endpoint URLs, oftentimes these strings can cover a whole monitor’s length of horizontal spacing, this will help to shorten that drastically.
Lambda improvements
The improvement to lambdas are very cool, let’s start off with the type inference:
Func<string> helloCodeheir = () => "Hello Codeheir reader"; // before
var helloCodeheir = () => "Hello Codeheir reader"; // after
The compiler is now clever enough to figure out the return type without having to explicitly state it. Talking of return types you can also return an explicit return type before the lambda itself:
var helloCodeheir = string? () => null;
There are a whole host of lambda improvements that I’ve not spoke about as I can’t imagine using them very often myself, but you may be different – click here for more information.
Conclusion
My favourite change in C# 10 has got to be the namespace improvements, I’m a sucker for vertical spacing improvements, especially in C#.
As stated there are plenty of other C# 10 features that I haven’t covered as I feel they’re a little more niche.
I hope you’ve enjoyed this blog – join our awesome community!