C# 6.0 has many new features that are available with the Visual Studio 2015. Here, we are mentioning some of the most highlighted features, that will make the developer's life much easier.
1. 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 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 = “123@afd.com” };
Employee employee2 = new Employee();
employee2.Name = “William Smith “;
2. 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; }
}
}
3. 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 = “123d@afd.com” };
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
{
4. public string Address { get; set; }
public string Email { get; set; }
}
}
More new features in
CSharp 6
SyedAdeel Ahmed / 5 days ago
6. The more new features in CSharp 6 will be available in Visual Studio 2015. There are
many exiting new features, that help in both the development and the productivity of the
software . Some main features include:
Initializers for properties
You can now add an initializer to an auto-property, pretty much as you can in a field:
public class DataCollected
{
public bool Married {get; set}=true;
public int Age { get; set; } = 25;
public float salary { get; set; } = 60000.12;
}
The initializer straightforwardly instates the field; it doesn’t work through the setter of the
property. The initializers are executed all together as composed, pretty much as; and
alongside with field initializers.
Much the same as field initializers, auto-property initializers can’t reference this – after
all they are executed before the object is legitimately initialized
Auto-properties concept
Properties can now be announced without a setter.
public class CarData
{
public int TotalCars { get; } = 20;
public string CarPrice { get; } = “2000$” ;
}
7. The support field of a getter-only auto-property is certainly pronounced as “readonly” . It
can be introduced through an initializer on the property as in the illustration above.
Additionally, a getter property can be assigned to a constructor also:
public class Myname
{
public string Name { get; }
public Myname(string fname, string lname)
{
Name = fname + ” + lname;
}
}
Have you seen this before previously? Surely not!
Methods and also user-characterized operators and transformations can be given an
expression body by utilization of the “lambda sign”. For example:
public Coordinates AddValues(int x1, int y1) => new Coordinates(x2 + x, y2 + y);
public class Person
{
public Person(float expenditures, float profits)
{
Expenditure = expenditures;
Profit = profits;
}
public float Expenditure { get; protected set; }
public float Profit { get; set; }
8. public void ProfitIncrease() => Profit++;
public float Expenditures() =>Profit- Expenditure;
}
Static members references
This feature permits all the available static members to be imported directly
in using statement when referring to the built in classes(for example).
using static System.Console;
using static System.Math;
using static System.DayOfWeek;
class System
{
static void Main()
{
WriteLine(Sqrt(9*9 + 2*2));
WriteLine(Saturday-Wednesday);
}
}
This is awesome feature introduced in C# 6.0