c# 8.0 top 5 Features
10 Apr

c# 8.0 top 5 Features

C# 8.0 was released with .NET Core 3.0. These two were released simultaneously in .NET Conf 2019

There are a lot of new features released as part of C# 8.0. But here I have picked the top 5.

Readonly members

 We have been using the readonly variable for a long time. Some of the advantages are,

  • It makes your class or struct immutable.
  • Objects become thread safe by default.
  • The state cannot be modified which means does not matter which thread using the object

Now we can apply readonly modifier to any members of a struct. This makes a lot of sense for struct as it’s a value type. Usage

  1. public struct Employee {  
  2.     public string FirstName {  
  3.         readonly get;  
  4.         set;  
  5.     }  
  6.     public string SecondName {  
  7.         readonly get;  
  8.         set;  
  9.     }  
  10.     public readonly string Name => $ “{FirstName} {SecondName}”;  
  11. }   

Using Declaration

 Using declaration has a shortcut which is pretty cool because it has eliminated the code block using the curly brackets. Instead of writing it the below way:

  1. public void GetEmployeeDetails(int employeeID) {  
  2.     using(var conn = new SqlConnection(“connectionstring”)) {  
  3.         //code for retrieving the employee details.  
  4.         //Connection will be disposed here as its internally a try- catch-finally block  
  5.     }  
  6. }   

Now we can write it as:

  1. public void GetEmployeeDetails(int employeeID) {  
  2.     using  
  3.     var conn = new SqlConnection(“connectionstring”);  
  4.     //code for retrieving the employee details.  
  5.     //Connection will be disposed  
  6. }  

Default Interface Method

 We can now add the default method to an interface. The idea is that we can add a method later to an interface with a default implementation so that existing implementations do not fail. I like the idea, but I’m not sure how  useful this feature will be and how much confusion it might create. It is an inline trait in other languages such as Scala. I’m not sure this will go well with SOLID principles. I might be wrong. If you folks have a better idea, please provide your comments. Usage

  1. public interface IPurchaseOrder {  
  2.     DateTime PurchaseDate {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     decimal Price {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     int Quantity {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     //default method  
  15.     decimal MarketPrice => Price * Quantity;  
  16. }  

Asynchronous streams

 In my option, this is one of the most exiting features. So far we have been returning only IEnumerable and it is useful as we can write LINQ queries on IEnumerable. Now we can create and consume streams asynchronously. Usage

  1. //AsynEnumberable usage  
  2. public static async IAsyncEnumerable < string > GenerateRandomValues() {  
  3.     Random rnd = new Random();  
  4.     for (int i = 0; i < 20; i++) {  
  5.         await Task.Delay(1000);  
  6.         yield  
  7.         return $ “A-{rnd.Next(1000)}”;  
  8.     }  
  9. }  

Invoke this method as below,

  1. await foreach(var n in GenerateRandomValues()) {  
  2.     Console.WriteLine($ “Random Value : {n}”);  
  3. }  

Property Pattern

 This is one of my absolute favorite  features. To be able to match on a property is priceless and the syntax is really easy. Let us look at the below example.

  1. public static string GetFamilyName(Employee employee) => employee  
  2. switch {  
  3.     {  
  4.         FirstName: “Prasad”  
  5.     } => “Pulickal”, {  
  6.         FirstName: “Pradeep”  
  7.     } => “Thekkeveli”, {  
  8.         FirstName: “Praveen”  
  9.     } => “PuthanMadam”,  
  10.     _ => “Not Available”  
  11. };  

Please look at the switch syntax, here “switch” comes after the variable address. In this example, if the Employee’s first name is “Prasad”, the family name will be “Pulickal” and so on. If anything else, the family name will be “Not Available”. Here anything else has been represented by an underscore (_) and I believe this is pretty standard in all languages for pattern matching. 

These are the top 5 features in my opinion. I would request you to share your viewpoints on these C# 8.0 features and provide your comments below. Thanks for reading the article and please do share what are your favorite features in C# 8.0.

About the Author

Comments are closed.