Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 15, 2021 10:13 am GMT

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.

C# 10 new features explained

Possible new C# 10features

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 Fieldkeyword

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 dotnetsafer = new{Name = "Dotnetsafer",Email = "[email protected]"};var bar = dotnetsafer with {Name = "Bar"};

Conclution:

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?

C# 10 listing new features


Original Link: https://dev.to/dotnetsafer/c-10-roadmap-exposing-new-features-2ifg

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To