C# 10: Interesting Features

When Microsoft announced that dot net will be opened sourced. developers community were very much interested. One of the key things that i personally liked that when google planned to launch Typescript, some of the features were taken with the help of Microsoft community where it got lot more momentum. I personally like C# and seen its growing since dot net framework 3.5, linq, C# 4 async prog, C# 5 static imports, string interpolation, wait in catch and finally blocks, tuples , records and so on. The momentum at which it is evolving has made the developers community crazy.

I was amazed at the features when it pronounced C# 10.

Namespace Announcement Change

We used something like these in our C#

This approach is admirably flexible. You can layer namespaces just by nesting blocks, a single file can hold types in any combination of namespaces, and multiple files can share the same namespace. The only price we pay for this system is a bit of extra indenting when compared to other curly bracket languages, like Java or JavaScript.

But what if there was a way to get the best of both worlds? In other words, to keep the flexible namespace block, but reduce the excessive indenting in cases when our code fits neatly into one namespace?

We can have only one file scoped namespace per file, which is fine as per the project hierarchy or simplicity we keep it.

It’s a minor feature, but sometimes easy gains are the best reward.

Primary Constructor

It is a feature that enables the developers to reduce the hand-written code. Most of the cases we have constructor , they don’t do anything more than copy parameters into properties, like this example Employee class. Constructors like these are so common that it’s no surprise that they eventually collect errors. Assigning a parameter to the wrong property (or to another parameter) is a surprisingly common mistake.

With a primary constructor, the basic constructor is generated automatically, using constructor arguments that are added to the class declaration. It looks like this:

We have not created any constructor, you can use it in the same way

Other things are also possible like calling a base constructor and including the validation check

Required keyword

To the further improvement, it will be possible to get rid of backing fields altogether. The new keyword field will provide access to said backing field.

class Person
{
public string name { get; init => field = value.Trim(); }

public DateTime dateOfBirth { get; set => field = value.Date; }
}

With

There will be new nifty enhancement with the anonymous types.

var foo = new
{
Name = “Foo”,
Email = “foo@mail.com
};
var bar = foo with {Name = “Bar”};

There is one feature that may or may not be

Caller argument exception: it is still under pipeline.

Full Stack Dot Net Developer, Software Enthusiatic, Developer, Learner