Target Typing And Shared Types In C# 9.0

Introduction

 
In today’s article we will look at Target Typing Improvements and Shared Types in C# 9.0. C# 9.0 has been introduced with .NET 5.0. We will look at another cool feature which C# 9.0 brings to us, which is target typing and shared types and how these can be used.

Using C# 9.0

 
Microsoft recently announced the availability of .NET 5 release candidate at Microsoft Ignite 2020. This included the latest features in C# 9.0. In order to code in .NET 5.0, we would need to install the latest preview of Visual Studio 2019 (Version 16.8.0 Preview 3.1). As I had read about some cool features in C# 9.0including the improvements to target typing and shared types, I downloaded and installed the required version as below,
 
Target Typing and Shared Types in C# 9.0
 
In previous versions of C#, if the return value types do not match, we get an error. Even if the types share the same type, we will still get anerror. However, in C# 9.0, if the return values have a shared type, there is no warning or error. Let us look at this with an example.
 

Using Target Typing and Shared Types

 
Let us create a console application in Visual Studio 2019 (Version 16.8.0 Preview 3.1) as below,
 
Target Typing and Shared Types in C# 9.0
 
Target Typing and Shared Types in C# 9.0
 
Target Typing and Shared Types in C# 9.0
 
Now, add the below code,
  1. using System;  
  2. using System.Collections.Generic;  
  3. namespace ConsoleAppTargetTyping {  
  4.     class Program {  
  5.         static void Main(string[] args) {  
  6.             var values = GetTaxPercentRates(true);  
  7.             foreach(var val in values) {  
  8.                 Console.WriteLine($ "Value is {val}");  
  9.             }  
  10.             Console.ReadKey();  
  11.         }  
  12.         public static IEnumerable < int > GetTaxPercentRates(bool IsSingle) {  
  13.             return IsSingle ? new List < int > {  
  14.                 5,  
  15.                 8,  
  16.                 11  
  17.             } : new [] {  
  18.                 2,  
  19.                 3,  
  20.                 5  
  21.             };  
  22.         }  
  23.     }  
  24. }  
Here you will see that there is an error indicated in the return statement. Although, both the List<int> and array are IEnumerable<int>, we cannot set them separately as done above. We get the below error,
 
Target Typing and Shared Types in C# 9.0
 
We can fix this code as below,
  1. using System;  
  2. using System.Collections.Generic;  
  3. namespace ConsoleAppTargetTyping {  
  4.     class Program {  
  5.         static void Main(string[] args) {  
  6.             var values = GetTaxPercentRates(true);  
  7.             foreach(var val in values) {  
  8.                 Console.WriteLine($ "Value is {val}");  
  9.             }  
  10.             Console.ReadKey();  
  11.         }  
  12.         public static IEnumerable < int > GetTaxPercentRates(bool IsSingle) {  
  13.             return IsSingle ? (IEnumerable < int > ) new List < int > {  
  14.                 5,  
  15.                 8,  
  16.                 11  
  17.             } : new [] {  
  18.                 2,  
  19.                 3,  
  20.                 5  
  21.             };  
  22.         }  
  23.     }  
  24. }  
Here we have casted the first value to IEnumerable<int> and all works fine.
 
Target Typing and Shared Types in C# 9.0
 
Next, we will change the properties of the project and set the Target Framework to .NET 5.0, as below,
 
Target Typing and Shared Types in C# 9.0
 
After we change the framework to .NET 5.0 and use C# 9.0, we can revert to the old code as below,
  1. using System;  
  2. using System.Collections.Generic;  
  3. namespace ConsoleAppTargetTyping {  
  4.     class Program {  
  5.         static void Main(string[] args) {  
  6.             Employee employee = new(1, "John Smith", 30);  
  7.             var values = GetTaxPercentRates(true);  
  8.             foreach(var val in values) {  
  9.                 Console.WriteLine($ "Value is {val}");  
  10.             }  
  11.             Console.ReadKey();  
  12.         }  
  13.         public static IEnumerable < int > GetTaxPercentRates(bool IsSingle) {  
  14.             return IsSingle ? new List < int > {  
  15.                 5,  
  16.                 8,  
  17.                 11  
  18.             } : new [] {  
  19.                 2,  
  20.                 3,  
  21.                 5  
  22.             };  
  23.         }  
  24.     }  
  25.     public class Employee {  
  26.         public int Id {  
  27.             get;  
  28.             set;  
  29.         }  
  30.         public string Name {  
  31.             get;  
  32.             set;  
  33.         }  
  34.         public int Age {  
  35.             get;  
  36.             set;  
  37.         }  
  38.         public Employee(int id, string name, int age) {  
  39.             Id = id;  
  40.             Name = name;  
  41.             Age = age;  
  42.         }  
  43.     }  
  44. }  
When we run the above code, we get the following output,
 
Target Typing and Shared Types in C# 9.0
 
Also, note that we have created an instance of the Employee class (although not used in this example) without specifying the type after the new keyword. This is because we specify the type in the left-hand side of the assignment statement. This is known as the target typed new expression. This is also a new feature of C# 9.0.
 

Summary

 
In this article, we looked at the improvements in target typing and shared types in C# 9.0. We could argue that this is not an incredibly significant change. However, these are the new changes which make C# a much more friendly language to program in. Happy Coding!