
Enterprise Application Development with C# 9 and .NET 5
By :

Target-typed expressions remove redundant mentions of the type when creating an object. With target-typed expressions, the type is inferred from the context it is used in. In C#, we use the new
keyword to instantiate an object by specifying the type. To create an object of StringBuilder
, the following is an example code snippet:
StringBuilder sb = new StringBuilder();
In this code snippet, the StringBuilder
type can be inferred from the context. With C# 9.0, using a target-typed expression, we can write the same code snippet as shown in the following example. Here, the StringBuilder
type is inferred:
StringBuilder sb = new ();
In the next section, we will learn how static anonymous functions help us to identify and address the unintended behavior of the program execution.