New features on c# 6.0

Microsoft released c# 6.0 and today we will talk about some new features of c# 6.0

Auto Property Initializers

We are able to set a value when initializers a property.

public string name {get; set;} = "Hamdi"
public string surName {get; set;} = "Ceylan"
public int age {get; set;} = 26


Primary Constructors

We can set first values while creating and object from class.

Car newCar = new Car("Honda",Civic);

Static Metod Without a Namespace

We are able to use static methods directly after defined method namespace in using block.

using System;
using System.Data;
using Math;//not a namespace just a static me

namespace TestApp
{
 class Program
 {
   static void Main(string[] args)
   {
    Console.WriteLine(Cos(0));
    //Cos Static Method coming from Math class
   }
 }
}

Dictionary Initializers

We are able o declare a dictionary with more readable style.

Dictionary<int,string> dic = new Dictionary<int,string>()
{
  [1] = "Pink",
  [2] = "Yellow",
  [3] = "Purple",
}

Monadic Null Checking

We are able to check object is null or not with ? operator.

string name;
var checkedName = name? ?? "No Name";

Await in Catch and Finally block

Before c# 6.0 we are able to use await keyword in try block but not in catch and finally block, in c# 6.0 we are able to use await in anywhere.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *