We will take a look at a few new cool features which are part of C# 10. These features might seem small, but they help to improve the overall readability and efficiency of our code. So, let us begin.
Creating a C# 10 console application
The first step is to create a simple C# console application in Visual Studio 2022 community preview edition as below.
Now, we add a new class called “Employee”.
Add the below code to the respective files.
Employee.cs
Program.cs
Let us analyze the new features we see in these two files. In the “Employee.cs” file, we see that we declare the namespace without the opening and closing brace, and hence the namespace is for the complete file. This helps to reduce indentation in the file.
In the “Program.cs” file, we are using the “global” keyword before the using statement. This is used when we want to use this using statement in many places and rather than repeating it in many files, we just declare it as global.
We also see that we can use both previously declared and assigned variables together when we deconstruct a key-value pair. This was not allowed before. Finally, we see that we can use string interpolation while declaring constants.
When we run this application, we see the below output.
No comments:
Post a Comment