Stay up to date on c# features

Barış Tutakli
2 min readApr 10, 2022

While learning new programming languages and technologies, it is vital to keep up to date on new features. In this article, I would like to talk about some features coming with C# 9 and C# 10 and C# 11(preview features)

I’d like to talk about some features such as “init only setters”, “Records”, ”Global Using Directives“, “Top-Level Statements” …

When we create classes depending on our needs, we create fields, properties, methods… After creating a class, there might be some properties that are preferred to be changed only at the instance creation time such as Id, creation time, etc. You can use “constructor” to prevent these changes. However, after C# 9 the init accessors enable us to do that without creating any constructors.

How to declare init accessors?

“Product.Title” can only be assigned in an object initializer.

Records

Records are immutable reference types and use value-based equality. They are used for copying and comparing data and are often used with DTOs.

How to create record types?

Example of Value Equality

Top-Level Statements

When we compare programming languages and talk about them, I’ve heard a lot of time about the number of codes written to do the same operation in python, c#, Java ...

There is another cool feature coming with C# 9 that removes unnecessary ceremony from many applications and decreases the number of codes. Let’s see what’s going on 🙂

Python program to print Hello World!

C# program to print Hello World!

Wow isn’t it?

Global Using Directives

If you created or developed .Net Core WebApp project, you would know or see _Viewİmports.cshtml. This file enables us to centralize directives and avoid adding the same using directives to all pages. The global using directive also enables you to use the same using directives without writing them again at the top of every file in your projects.

Return Type of a Lambda Expression

Lambda expressions allow us to create anonymous functions. With C# 10 release, we can specify the return type of a lambda expression as I showed you in the code below. Invoke method is used to execute anonymous functions.

String Interpolation

A cool feature is coming with C# 11. Interpolated expressions can contain newlines that make it more readable to format strings. For instance:

Would you like to help me to better my articles?🙂 See you next time!

--

--