In this blog, we will dive into the favorite language of our .NET Team, which is C#! What are the features that we love to use? And how does it improve our day-to-day work?
#5 - Required properties
Object Initializers were introduced when C# 6 was released, giving us a way to assign values of accessible fields without having to use a constructor, for example:
Blog asset
However, if you want to ensure that one or more properties always have a value assigned, you will need to use a constructor with a mandatory parameter.
In C# 10 we can use required properties; in the following example, we make sure that the Name is required to have a value assigned:
Blog asset
Now the build will fail when you try to instantiate the class without assigning the Name property! This feature will help us avoid null reference mistakes and gives less code to cover with unit tests since we don’t have to write the constructors that we needed before.
#4 - File-scoped namespaces
Namespaces have been part of C# for a really long time, and ever since it was introduced, you can define types in namespaces by placing the type inside the body of a namespace. Since you have to place the type inside the body of a namespace, then all of the code in the namespace will be indented. But with C# 10, this will be a thing of the past! Now you can define a namespace that applies to all types in the file and save you whitespace at start of each line.
Blog asset
This is mostly only a visual improvement, giving you a bit more screen space to write more code. 😊
#3 - Global using directives
When you write code in C#, you can make your life easier by adding using’s to the top of the file. This way, you get direct access to the types within the namespaces without specifying their namespace.
However, in C#, they made it more DRY, because now (Like the Razor equivalent). It allows you to specify global using’s, which you only have to specify once, and those namespaces will be “used” in the entire project! That saves a lot of time specifying the System namespace or cleaning up unused using’s.
Blog asset
This will result in all types within the Rebels.Rampage.Sdk will be available across all files in the project, or more technically, all types in the build. You don’t need a special _Imports.cs file or something, but my suggestion is that it would be wise to specify all the global using’s in a single, separated file.
#2 – Field keyword
Another excellent feature for C# is the field keyword. This keyword helps with accessing the internal field of a C# property.
Currently, we are spoiled with the auto-implemented properties of C#, but sometimes you need to access the internal property. Most of the time, we need access to this property for lazy loading of the values.
The C# 9 way:
Blog asset
The C# 10 way:
Blog asset
You don’t have to specify the backing field yourself anymore. Also, the init keyword has been added, which can be used to determine the initial value via a statement. We could already specify initial values in a constant way, but now it is dynamic.
Blog asset
Now when the Created property is initialized with today’s date in string form.
I like this new feature since we can keep the logic of the property at the property itself, instead of adding helper code for the property somewhere else in the class. This makes the code more readable and easier to maintain.
#1 – Null parameter checking
How many times do we have to check if the required object parameters of a method are not null? And then throw an ArgumentNullException, and add a unit test to test this path. This is now of the past with C#10!
C# 9:
Blog asset
And this is how we do it in C#10:
Blog asset
Just add two exclamation points, and we are done!
I think this will save a lot of time and headaches for developers, and even more, it makes the code more readable since it is shorter. This example has only one parameter, but imagine a method with 5 parameters + 5 conditions will make the body of the method considerably longer.
At Rebels we do not only want to work harder, but also smarter. By always staying on top of the latest developments in our field, we try to find the best features that ensure that we always get the best results for our clients. Would you like to read more about the features that are added to C#10? Read more about it here.
Which features are your top 5? Do you think that we missed an important feature? Let us know by contacting us on LinkedIn!
    Author's experience: Ronald Baltus, Senior .NET Developer
At Rebels, we have many highly-skilled international employees interested in the Dutch language...
Recently, we had a tech talk with Ingmar van Busschbach about Unreal Engine 5. Today, we will be diving deeper into the topic.
Our new colleague Ivanna shares about her unique onboarding experience at Rebels.
Virtual reality is making its way into industries beyond gaming, which will define the future and growth of VR.