C# 6.0 New Feature

C# 6.0 New Feature



How to get C# Updated Version?
Two ways we can update C# 6.0
Updated Visual Studio into VS 2014
Or
Installing Roslyn Package in VS 2013

$ Sign :

Its simplify String indexing

Example:

var value = new Dictionary() {
// using inside the initialize
$first = "Nikhil"
}

Assign value to member

C# 5.0
 C# 6.0
Value[first] =”Nikhile”
Value. $first = “Nikhil”

Exception Filters:

Exception filter already support in Vb.net now C#.net as well start to support .Exception filter is nothing but in catch block we can specify If Condition.
IF condition is fail inside catch block statement it won’t execute.

         C# 5.0
 C# 6.0
          try
            {       }
            catch (Exception ex)
            {       }

  Try  {
     throw new Exception("Null Error");
          }
catch (Exception ex) if (ex.Message == " IndexError")
            {
                // this one will not execute.
            }
            catch (Exception ex) if (ex.Message == " Null Error ")
            {
             // this one will execute
            }

await in Catch block and finally block :

Previous version C# not support await keyword in catchblock and finally block ,Now in c# 6.0 start supporting

         C# 5.0
 C# 6.0

            try
            {
                DoSomething();
            }
            catch (Exception)
            {
             //Its not Support here
                await LogService.LogAsync(ex);
            }

            try
            {
                DoSomething();
            }
            catch (Exception)
            {
               // New feature it will support
                await LogService.LogAsync(ex);
            }

Declaration Expression:

This feature allows you to declare local variable in the middle of an expression. It is as simple as that but really destroys a pain.

         C# 5.0
 C# 6.0
Int  Qty;
Int.TryParse(txtnumber.Text, out Qty)

Int.TryParse(txtnumber.Text, Int out Qty)

Auto Property Initialize:

In C# 6.0 added new feature called auto propertyinitialize like below

public class Person
{
// You can use this feature on both
//getter only and setter / getter only properties
public string FirstName { get; set; } = "Nikhil";
public string LastName { get; } = "Jagathesh";
}

Primary constructors:

A primary constructor allows us to define a constructor for a type and capture the constructor parameters to use in initialization expressions throughout the rest of the type definition. Here’s one 

simple example:

//this is primary constructor

public Class Money(string currency, decimal amount)
{
public string Currency { get; } = currency;
public decimal Amount { get; } = amount;
}

Dictionary Initialize:

         C# 5.0
 C# 6.0
Dictionary country = new Dictionary()
            {
                { "India", "TN" },
                { "United States", "Washington" },
                { "Some Country", "Some Capital " }
            };
Dictionary country = new Dictionary()
            {
                // Look at this!
                ["Afghanistan"] = "Kabul",
                ["Iran"] = "Tehran",
                ["India"] = "Delhi"
            };

Null-Conditional Operator

This is an exception that almost always indicates a bug because the developer didn’t perform sufficient null checking before invoking a member on a (null) object.

Look below new feature very interesting

         C# 5.0
 C# 6.0
public static string Truncate(string value, int length)
{
  string result = value;
//this pain work for us
  if (value != null) // Skip empty string check for elucidation
  {
    result = value.Substring(0, Math.Min(value.Length, length));
  }
  return result;
}
public static string Truncate(string value, int length)
{         
  return value?.Substring(0, Math.Min(value.Length, length));
}




0 Comments

Featured Post

How to learn Azure without paid subscription

Microsoft Azure is a cloud service that is constantly expanding to help your organization meet business challenges. You can build, manage d...

Subscribe to the FREE Weekly Newsletter to receive all the latest Microsoft Azure, AI, Xamarin

Popular Posts