π About the possible new features of C#Β 10
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.
β 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 π
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; }
}
πΌ C# 10 File-level namespaces
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{
}
πΌ 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 π
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.
πΌ 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 π
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 ByteHide = new
{
Name = "ByteHide",
Email = "ByteHide@mail.com"
};
var bar = ByteHide with {Name = "Bar"};
π’ Conclution:
To finish this article, from ByteHide 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?π€
Top comments (8)
While I love the new features that get introduced into the language, when maintaining large code bases it can become problematic. Do you know of any tools or compiler functions that can help with the job of migrating a code base to the newer standard? I prefer to do this in bigger chunks rather than during refactoring when adding features to make the commits cleaner and rollback easier if bugs are found.
Jetbrains Resharper or Rider can help with some of those changes. For example converting null checks into safe traversal via '?' can be done en masse with a click of a button. You'll still need to review the code once though to be on the safe side.
Required properties & lambda enhancements are probably my favourite upcoming features.
Nullable reference types aren't really usable without required properties as you needlessly have to add constructors to stop compiler from complaining. Really looking forward to it & finally able to turn nullable references on for projects.
Yes, that seems very useful to us too!
It looks very fresh, I hope Microsoft does not delay more than the announced date to release all the news such as C # 10, .NET MAUI, Visual Studio 2022 and I'm sure I will be missing some. Because these updates will greatly improve the development of any .NET application. Especially the Hot Reload, it promises a lot and we hope it does not have too many bugs and works correctly. All the best
C# team: How can we make language even more complex, add new keywords every new release?
Hahaha, it can look like this too, but it doesn't really make it more complex, in some cases it simplifies development, it is true that you need more and more knowledge and study to be a good .NET developer.
I'm not a .NET developer. But I do love C#. If we thought from beginner perspective, it would make learning much harder.