20
C# 10 Roadmap β Exposing NEW features
A few days ago Mads Torgersen, the lead designer of the C# language at Microsoft, outlined the cool new things that C# 10 will have.
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.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--G8QhPGJ0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AF5qsSdo2ZFSTn4tPHEZmTg.png)
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 π
public record Employee
{
public string Name { get; init; }
public decimal Salary { get; init; }
public DateTime Date{ get; init; }
}
When instantiating lightweight objects, we always prefer to do it quickly with the object initializer syntax π
var theNewGuy = new Employee
{
Name = "Chris Smith",
Salary = 1000m,
Date = DateTime.Now()
};
Okay
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 π
public record Employee
{
public required string Name { get; init; }
public decimal Salary { get; init; }
public DateTime Date{ get; init; }
}
Any C# programmer knows that even the simplest program uses a block structure for namespaces π
namespace HelloWorld
{
class Hello
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
}
This is very flexible as you can overlap namespaces by simply nesting blocks. The only problem is that we add a bit of extra indentation when compared to other languages like Java or JavaScript.
The question we ask ourselves at this point is:
Is it possible to keep that functionality, but at the same time reduce excess indentation? π€
Yes β
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 π
NameSpace HelloWorld; public class Hello
{
static void Main (string [] args)
{
System.Console.WriteLine ("Β‘Hello World!");
}
}
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 π
namespace Company.Product;
Company.Product.Componentnamespace Component{
}
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 π
public record Employee
{
public required string Name { get; init; }
public decimal Salary { get; init; }
public DateTime Date{ get; init => field = value.Date(); }
}
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 π
private string _firstName;public string Name
{
get
{
return _tName;
}
set
{
if (value.Trim() == "")
throw new ArgumentException("No blank strings");_Name = value;
}
}
Now you can use an autoimplemented property and field π
public string tName {get;
set
{
if (value.Trim() == "")
throw new ArgumentException("No blank strings"); field = value;
}
}
This is as long as there is no need to change the data type, as there is no need to declare the backing field.
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 π
class Person
{
public required string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}
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 π
class Person
{
public Person(string name) => Name = name;
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}
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.
class Person
{
public string Name { get; init => field = value.Trim(); }
public DateTime DateOfBirth { get; set => field = value.Date; }
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.
var dotnetsafer = new
{
Name = "Dotnetsafer",
Email = "[email protected]"
};
var bar = dotnetsafer with {Name = "Bar"};
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?π€
![](https://res.cloudinary.com/practicaldev/image/fetch/s--fmiAUoL0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A9CMTTTizyzFMnirnIZYi-Q.png)
20