Use Target Typed New Expressions to infer type on the right in C# 9 +

In C# 9 Target Typed New Expressions were introduced.

They allow us to infer type on the right, whereas var allows us to infer type on left. The simple example below shows how we can omit the type when using ‘new()’ so the code becomes a little cleaner.

Click on the image for a larger view in a new window.

Personally I prefer to use TTNE over var as we (mostly) read from left to right so I can see the type sooner.

Have you used Target typed new expressions yet?


Need Help with Your C# Projects?

We offer expert support and development services for projects of any size. Contact us for a free consultation and see how we can help you succeed.

CONTACT US NOW