About the possible new features of C# 10
One of the biggest benefits of open source software is being able to see how your project evolves over time as the days go by. With this we want to refer to the same C#, since we can follow its progress on GitHub and see its main news.
Possible new C# 10 features
C# 10 Required properties
Previously, to ensure that objects were created correctly, class constructors were always used. Today we have the possibility of using lighter constructions, such as the self-implemented properties as in this registry 👇
When instantiating lightweight objects, we always prefer to do it quickly with the object initializer syntax 👇
But what if the object doesn’t make sense until some properties are set? 🤔
You could add a constructor, but you would have to add more standard text. Apart from copying parameter values to properties.
In C# 10 this problem disappears 👇
C# 10 File-level namespaces
Any C# programmer knows that even the simplest program uses a block structure for namespaces 👇
The question we ask ourselves at this point is:
Is it possible to keep that functionality, but at the same time reduce excess indentation? 🤔
How is it possible? 🤔
It just opened that when entering file-scoped namespaces, this would allow setting a default namespace that would automatically apply to the entire file by removing the indentation 👇
Suppose we add a namespace block to a file using a file scoped namespace, just create a nested namespace.
Let’s look at a quick example 👇
C# 10 Field keyword
After quite some time, the entire C# development team has managed to optimize the code. Self-deployed properties are great, but they can only take you so far.
Many times you are forced to add the backing field to your class and write the property methods as usual.
In the C# 10 new features, there is a new backdoor with the field keyword, which exposes the automatically created backing field 👇
The cleaning code looks very good, very simple and almost declarative. The best part is that you can use the field keyword to access the backing field in any process, be it set, init, or get.
Let’s see how a property would be valid in an ordinary class 👇
Now you can use an autoimplemented property and field 👇
This is as long as there is no need to change the data type, as there is no need to declare the backing field.
C# 10 Objects initialisation
One of the goals the C# team is focussing on, is making initialisation of objects easier. That is why it will be possible to flag properties of a class, struct, record or record struct as required. It makes those properties mandatory to fill in.
Lets see 👇
This can be done via a constructor, or this can be done with object initialisation. The two class definitions below are equivalent. If you write it with the required keyword, you cannot instantiate the Person without setting the Name property.
The compiler will throw errors and fail to compile 👇
To further improve properties, it will be possible to get rid of backing fields alltogether. The new keyword field will provide access to said backing field.
This will be available for both setters as init only properties.
There will be a few nifty little enhancements in the next version as well. One is that the with operator will support anonymous types as well.
To finish this article, from Dotnetsafer, our conclusion is that C# still has many years of travel ahead of it and it still has many things to add to make the task of programming even easier and more optimal.
What do you think?🤔