What is new in C Sharp 6.0 ?






What is new in C Sharp 6.0 ?

We will discuss what is new in C Sharp 6.0 that is bringing more attention towards the programming communities.
In this article, I’ll display the new components and give a report about the new features that are attracting the C# developers around the world.

Redefining the null conditional operator “?”

Indeed, even the most up to date .NET engineers are likely acquainted with the NullReferenceException. This is an exemption that quite often shows a bug in light of the fact that the engineer didn’t perform adequate null value checking before calling a member on an object that has (null) value.

The null propagation operator

C# 6.0 presented numerous new elements in Visual Studio 2015 Review. How about, if we observe one of its new elements, the null propagation operator ? In prior adaptations of the C# dialect(language), we generally needed to compose an if condition for an invalid check before utilizing an object or its properties. Presently C# 6.0 has presented the Null-Propagation Operator (?.) that empowers engineers to check for the null inside of an object references. The null-propagation operator (?.) will return null if anything in the object references is null. We require a superior method for taking care of null exception cases where propagation of null exists. The operator can now be utilized like “nullable types” by simply using a question mark (?) after the method instance before calling the property. We don’t have to compose extra if statements to check for the nulls.

Examples

The Old-School approach for using multiple if else checks:

using System;

namespace Csharp6NullPropagation
{
class Employees
{
static void Main()
{
Employee employee1 = new Employee();
employee1.Name = “Adeel Ahmed”;
employee1.ID = 23;
employee1.contacts = new Contact()
{ Address = “ABC Street House XYZ”, Email = “[email protected]” };

Employee employee2 = new Employee();
employee2.Name = “William Smith “;
employee2.ID = 52;
employee2.contacts = new Contact() { Address = null, Email = null };

Console.WriteLine(“Employee 1:\n”);
if (employee1.Name == null) Console.WriteLine(“No Name Provided”);
else Console.WriteLine(employee1.Name);
if (employee1.contacts.Address == null) Console.WriteLine(” No Address Provided”);
else Console.WriteLine(employee1.contacts.Address);

Console.WriteLine(“\nEmployee 2:\n”);
if (employee2.Name == null) Console.WriteLine(“No Name Provided”);
else Console.WriteLine(employee2.Name);
if (employee2.contacts.Email == null) Console.WriteLine(“No Email Provided”);
else Console.WriteLine(employee2.contacts.Email);
if (employee2.ID == null) Console.WriteLine(“No ID Provided”);
else Console.WriteLine(employee2.ID);
Console.ReadLine();
}
}
class Employee
{
public string Name { get; set; }
public int ID { get; set; }
public Contact { get; set; }
}
class Contact contacts
{
public string Address { get; set; }
public string Email { get; set; }
}
}

Now, the null propagation operator in C# 6.0 makes the developer’s life much easier.

Trendy use of null propagation operator

using System;

namespace Csharp6NullPropagation
{
class Employees
{
static void Main()
{
Employee employee1 = new Employee();
employee1.Name = “Adeel Ahmed”;
employee1.ID = 23;
employee1.contacts= new Contact()
{ Address = “ABC Street House XYZ “, Email = “[email protected]” };

Employee employee2 = new Employee();
employee2.Name = “William Smith”;
employee2.ID = 52;
employee2.contacts = new Contact()
{ Address = null, Email = null };
WriteLine(“Employee 1:\n”);
WriteLine(employee1?.Name ?? “No Name”);
WriteLine(employee1?.contacts?.Address ?? “No Address”);
WriteLine(employee1?.contacts?.Email ?? “No Email provided”);
WriteLine(“\nStudent 2:\n”);
WriteLine(employee2?.Name ?? “No Name”);
WriteLine(employee2?.contacts?.Address ?? “No Address”);
WriteLine(employee2?.contacts?.Email ?? “No Email provided”);
ReadLine();
}
}
class Employee
{
public string Name { get; set; }
public int ID { get; set; }
public Contact contacts { get; set; }
}
class Contact contacts
{
public string Address { get; set; }
public string Email { get; set; }
}
}


Syed Adeel Ahmed
Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Leave a Reply