Abigale  Yundt

Abigale Yundt

1599631740

C# 9.0: Target-typed New Expressions – Make Your Initialization Code Less Verbose

In the previous blog posts you learned about different C## 9.0 features:

In this blog post, let’s look at another very interesting feature of C## 9.0 that is called target-typed **new** expressions.

Target-typed means that an expression gets the type from the context it is used in. With C## 9.0 the new expression gets the type from the context, which means you don’t have to specify the target-type explicitly to call a constructor. Let’s look at this with a simple example.

Creating New Objects Before C## 9.0

Let’s define the Friend class that you see in the code snippet below. As you can see, it has a default constructor and also a constructor with the parameters firstName and lastName to initialize the properties FirstName and LastName.

public class Friend
{
    public Friend() { }

    public Friend(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
}

To create a new instance of this Friend class, you can use this statement:

Friend friend = new Friend();

You can also use the constructor overload to pass in a firstname and a lastname:

Friend friend = new Friend("Thomas", "Huber");

As the variable gets initialized directly with a new Friend, you can also use the var keyword for the variable declaration like below. The C## compiler detects in this case the type Friend for the variable, as you assign a new Friend instance to it. The var keyword was introduced in 2007 with C## 3.0 and .NET Framework 3.5, and it works for local variables.

var friend = new Friend("Thomas", "Huber");

Now, in all those cases above you can see that the new expression – that’s the part on the right side of the = sign – always requires the class name. That’s not the case anymore with C## 9.0 if the target-type is already known from the left side of the = sign, which is always the case if you don’t use the var keyword on the left side of the = sign.

Use Your First Target-typed New Expression

With C## 9.0 you can create a new Friend like below with the target-typed new expression to call the default constructor. Note that I don’t specify the Friend class on the right side of the = sign, the compiler gets it from the left side of the = sign:

Friend friend = new();

You can also call the overloaded constructor of the Friend class by passing in a firstname and a lastname:

Friend friend = new("Thomas", "Huber");

But, of course, you can not use the target-typed new expression when you use the var keyword to declare your variable, because then the compiler does not have a chance to find out the type that you want to create. So, the following statement does not work:

var friend = new("Thomas", "Huber"); // does NOT work

#.net #c# #c++

What is GEEK

Buddha Community

C# 9.0: Target-typed New Expressions – Make Your Initialization Code Less Verbose
Abigale  Yundt

Abigale Yundt

1599631740

C# 9.0: Target-typed New Expressions – Make Your Initialization Code Less Verbose

In the previous blog posts you learned about different C## 9.0 features:

In this blog post, let’s look at another very interesting feature of C## 9.0 that is called target-typed **new** expressions.

Target-typed means that an expression gets the type from the context it is used in. With C## 9.0 the new expression gets the type from the context, which means you don’t have to specify the target-type explicitly to call a constructor. Let’s look at this with a simple example.

Creating New Objects Before C## 9.0

Let’s define the Friend class that you see in the code snippet below. As you can see, it has a default constructor and also a constructor with the parameters firstName and lastName to initialize the properties FirstName and LastName.

public class Friend
{
    public Friend() { }

    public Friend(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
}

To create a new instance of this Friend class, you can use this statement:

Friend friend = new Friend();

You can also use the constructor overload to pass in a firstname and a lastname:

Friend friend = new Friend("Thomas", "Huber");

As the variable gets initialized directly with a new Friend, you can also use the var keyword for the variable declaration like below. The C## compiler detects in this case the type Friend for the variable, as you assign a new Friend instance to it. The var keyword was introduced in 2007 with C## 3.0 and .NET Framework 3.5, and it works for local variables.

var friend = new Friend("Thomas", "Huber");

Now, in all those cases above you can see that the new expression – that’s the part on the right side of the = sign – always requires the class name. That’s not the case anymore with C## 9.0 if the target-type is already known from the left side of the = sign, which is always the case if you don’t use the var keyword on the left side of the = sign.

Use Your First Target-typed New Expression

With C## 9.0 you can create a new Friend like below with the target-typed new expression to call the default constructor. Note that I don’t specify the Friend class on the right side of the = sign, the compiler gets it from the left side of the = sign:

Friend friend = new();

You can also call the overloaded constructor of the Friend class by passing in a firstname and a lastname:

Friend friend = new("Thomas", "Huber");

But, of course, you can not use the target-typed new expression when you use the var keyword to declare your variable, because then the compiler does not have a chance to find out the type that you want to create. So, the following statement does not work:

var friend = new("Thomas", "Huber"); // does NOT work

#.net #c# #c++

Let's Give Some Unit Testing Love to C# 8 and C# 9 Features

According to StackOverflow, C## is one of the most-loved programming languages. And I completely understand that—it is powerful, easy to learn and consistently improving and developing. It is a living language. :)

The last couple of years, there were new features added to the languages, and the new versions keep coming up—C## 7, C## 8, C## 9.

As you know, we at Progress Telerik are proud that our products are always in sync with the latest things in the .NET world, and C## 9 and JustMock are no exception.

#c #c# #c#8 #c#9

Generics type in C# | Generic Class | Generic Method | C# Bangla Tutorial | Advanced C#

https://youtu.be/xGjX0R_6qDg

#oop in c# #object oriented programming in c# #object oriented concept in c# #learn oop concept #advance c# #generics type in c#

Generics type example in C# | Generic Class | Generic Method | C# Tutorial | Advanced C#

https://youtu.be/xfDjyg9jKSk

#oop in c# #object oriented programming in c# #object oriented concept in c# #learn oop concept #advance c# #generics type example in c#

Juanita  Apio

Juanita Apio

1618251060

C# 9.0: Covariant Return Types – Specify More Specific Return Types

In C#, you can return since version 1.0 more specific types from properties and methods. Of course, object-oriented languages like C## let you do that. When a programming language let’s you express that more specific return type, this is called covariance. And exactly that covariance is added in C## 9.0 for the return types of overridden methods and properties.

#.net #c# #c# 9.0