More new features in CSharp 6




Evolution of C#

Image credit: kumarvikram.com

More new features in CSharp 6

More new features in CSharp 6 will be available in Visual Studio 2015. There are many exciting new features, which will 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 instantiates the field; it doesn’t work through the setter of the property. The initializers are executed all together and also 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 written without a setter.

public class CarData

{

public int TotalCars { get; } = 20;

public string CarPrice { get; } = “2000$” ;

}

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 user-defined operators 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; }

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 which is introduced in C# 6.0


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