I have seen in recent past Microsoft has brought back lot of legacy VB stuff into C#. If you remember VB provides with clause , this features bring back memory of that.
Dim theCustomer As New Customer
With theCustomer
.Name = "Coho Vineyard"
.URL = "http://www.cohovineyard.com/"
.City = "Redmond"
End With
Explanation
Lets say we have a Customer class with a property of type CustomerDetails as below
public class Customer
{
public CustomerDetails CustomerDetails { get; set; }
}
public class CustomerDetails
{
public string CustomerName { get; set; }
}
Many a times we need to write a code to set CustomerName property as below with proper null checks.
//Prior to C# 6
private void SetCustomerName(Customer customer)
{
if ( customer !=null && customer.CustomerDetails!=null)
customer.CustomerDetails.CustomerName= "Shakti";
}
//With C# 6.0
private void SetCustomerName6(Customer customer)
{
customer?.CustomerDetails?.CustomerName ?? "Shakti";
}