What is new in C# 6

By | November 23, 2015

So what is new in C# 6.0? I am documenting the new features in C# 6.0 simply to keep myself up to date and maybe someone else will also benefit.

  • nameof
    Used to obtain the simple (unqualified) string name of a variable, type, or member.Now  this is a cool little feature. Especially useful when you wish to do some logging. nameof allows you to get the name of the variable, type or member without having to code a string.It simply allows you to output the ‘name’ not the value or the type of the object in question.
    using System;namespace MyNamespace
    {public class MyCoolClass
    {
    private void TakeOverTheWorld(Object o)
    {
    Console.WriteLine(nameof(this.TakeOverTheWorld)); // Will output ‘TakeOverTheWorld’
    Console.WriteLine(nameof(o)); // Will output ‘o’
    }
    }
    }
  • String interpolation
    Used to construct strings. An interpolated string expression looks like a template string that contains expressionsIts simply another way to format strings. It enhances the readability of the code somewhat by allowing you to include the variables as part of the string, see the example below.
    public void InterpolateString()
    {
    string Name = “Joe”;string CompositeString = string.Format(“Hello! My name is {0}”, Name);
    string InterpolatedString = $”Hello! My name is {Name}”;
    }
  • Null-conditional member access and indexing
    Used to test for null before performing a member access (?.) or index (?[) operation.This is a rather handy new feature. It allows you to do a null reference check and return null at the same time. Pretty handy if you have to access a property of a object that might be several layers into an Object graph.
    public void SomeMethod()
    {
    int? count = User?First?.Jobs?.Count(); // null if users or jobs is nullif (count == null)
    {
    // Do something
    }
    }
  • Object initializers
    Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements.
    Person person = new Person{ Name = “James”, Surname = “Kirk”, Rank = Rank.Captain};
  • Collection initializers & add extension methods
    Collection initializers let you specify one or more element initializers when you initialize a collection class
    Person captain = new Person{ Name = “James”, Surname = “Kirk”, Rank = Rank.Captain};
    Person officer = new Person{ Name = “S’chn T’gai”, Surname = “Spock”, Rank = Rank.ScienceOfficer};
    List enterpriseStaff = new List { captain, officer };
  • Overload resolution
    I must be honest, I am not sure what this is. The only description just says.The compiler has improved overload resolution that results in more code just working the way you would expect it to behave. One place where you might stop noticing a problem is when choosing between overloads taking nullable value types, or when passing method groups (instead of lambdas) to overloads that take delegates.
  • Exception filters
    Now this is a cool feature.The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions.
    try
    {
    ….
    }
    Catch (ArgumentException e) if (e.ParamName == “…”)
    {
    }
  • Await in catch and finally blocks
    The completed task to which await is applied might be in a faulted state because of an unhandled exception in the method that returns the task. Awaiting the task throws an exception.
  • Auto-property InitializersThis is a cool feature that I will definitely use.

    you can initialize auto-implemented properties similarly to fields

    public string FirstName { get; set; } = “Jane”;

  • Using / Import of Static Methods

    You can import accessible static members of static types so that you can refer to the members without qualifying the access with the type’s name.

using static System.Console;
using static System.Math;
class Program
{
static void Main()
{
WriteLine(Sqrt(3*3 + 4*4));
}
}


Reference:
What’s New for Visual C#
C# : How C# 6.0 Simplifies, Clarifies and Condenses Your Code

Leave a Reply

Your email address will not be published.