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
- public struct Employee {
- public string FirstName {
- readonly get;
- set;
- }
- public string SecondName {
- readonly get;
- set;
- }
- public readonly string Name => $ "{FirstName} {SecondName}";
- }
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:
- public void GetEmployeeDetails(int employeeID) {
- using(var conn = new SqlConnection("connectionstring")) {
-
-
- }
- }
Now we can write it as:
- public void GetEmployeeDetails(int employeeID) {
- using
- var conn = new SqlConnection("connectionstring");
-
-
- }
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
- public interface IPurchaseOrder {
- DateTime PurchaseDate {
- get;
- set;
- }
- decimal Price {
- get;
- set;
- }
- int Quantity {
- get;
- set;
- }
-
- decimal MarketPrice => Price * Quantity;
- }
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
-
- public static async IAsyncEnumerable < string > GenerateRandomValues() {
- Random rnd = new Random();
- for (int i = 0; i < 20; i++) {
- await Task.Delay(1000);
- yield
- return $ "A-{rnd.Next(1000)}";
- }
- }
Invoke this method as below,
- await foreach(var n in GenerateRandomValues()) {
- Console.WriteLine($ "Random Value : {n}");
- }
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.
- public static string GetFamilyName(Employee employee) => employee
- switch {
- {
- FirstName: "Prasad"
- } => "Pulickal", {
- FirstName: "Pradeep"
- } => "Thekkeveli", {
- FirstName: "Praveen"
- } => "PuthanMadam",
- _ => "Not Available"
- };
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.