DEV Community

Cover image for C# 10 Roadmap βœ… Exposing NEW features
ByteHide
ByteHide

Posted on • Updated on • Originally published at bytehide.com

C# 10 Roadmap βœ… Exposing NEW features

πŸ”„ 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; }
 }
Enter fullscreen mode Exit fullscreen mode

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()
 };
Enter fullscreen mode Exit fullscreen mode

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; }
 }
Enter fullscreen mode Exit fullscreen mode

πŸ”Ό 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!");
  }
  }
 }
Enter fullscreen mode Exit fullscreen mode

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!"); 
 } 
 }
Enter fullscreen mode Exit fullscreen mode

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{
 }
Enter fullscreen mode Exit fullscreen mode

πŸ”Ό 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(); }
 }
Enter fullscreen mode Exit fullscreen mode

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;
}
}
Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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; }
}
Enter fullscreen mode Exit fullscreen mode

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; }
}
Enter fullscreen mode Exit fullscreen mode

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; }
Enter fullscreen mode Exit fullscreen mode

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"};
Enter fullscreen mode Exit fullscreen mode

🟒 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?πŸ€”

Image description

Oldest comments (8)

Collapse
 
glsolaria profile image
G.L Solaria

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.

Collapse
 
salmanbabri profile image
salmanbabri

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.

Collapse
 
zaichengpao profile image
zaichengpao

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

Collapse
 
salmanbabri profile image
salmanbabri • Edited

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.

Collapse
 
bytehide profile image
ByteHide

Yes, that seems very useful to us too!

Collapse
 
sramisy profile image
sramisy

C# team: How can we make language even more complex, add new keywords every new release?

Collapse
 
bytehide profile image
ByteHide • Edited

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.

Collapse
 
sramisy profile image
sramisy

I'm not a .NET developer. But I do love C#. If we thought from beginner perspective, it would make learning much harder.