Saturday, September 7, 2019

C# Feature Details

In this post, would like to provide the new features introduced between major version releases of C# as below:

Version 7.0
        Released on: March, 2017         Target Framework: 4.6.2         Visual Studio version: 2017
  • Out variable: In this improved version, you can now declare out variables in the argument list of a method call, rather than writing a separate declaration statement above the line and assign an initial value.
    if (int.TryParse(input, out var answer))
            Console.WriteLine(answer);
    else
            Console.WriteLine("Could not parse input");
  • Tuples and Deconstruction: Tuples are lightweight data structures that contain multiple fields to represent the data members. The fields aren't validated, and you can't define your own methods. A tuple can be created by assigning a value to each member, and optionally providing semantic names to each of the members of the tuple.
    (string Alpha, string Beta) namedLetters = ("a", "b");
    Console.WriteLine($"{namedLetters.Alpha}, {namedLetters.Beta}");
    In a tuple assignment, you can also specify the names of the fields on the right-hand side of the assignment as below:
    var alphabetStart = (Alpha: "a", Beta: "b");
    Console.WriteLine($"{alphabetStart.Alpha}, {alphabetStart.Beta}");
    There may be times when you want to unpackage the members of a tuple that were returned from a method. You can do that by declaring separate variables for each of the values in the tuple. This unpackaging is called deconstructing the tuple. It looks as below:
    (int max, int min) = Range(numbers);
    Console.WriteLine(max);
    Console.WriteLine(min);
  • Pattern matching
  • Local functions
  • Expanded expression bodied members
  • Ref locals and returns
  • Discards
  • Binary Literals and Digit Separators
  • Throw expressions
Version 6.0
        Released on: July, 2015         Target Framework: 4.6         Visual Studio version: 2015
  • Static imports
  • Exception filters
  • Auto-property initializers
  • Expression bodied members
  • Null propagator
  • String Interpolation
  • nameof operator
  • Index initializers
Version 5.0
        Released on: August, 2012         Target Framework: 4.5         Visual Studio version: 2012 and 2013
  • Asynchronous members
  • Caller info attributes
Version 4.0
        Released on: April, 2010         Target Framework: 4.0         Visual Studio version: 2010
  • Dynamic binding
  • Named/optional arguments
  • Generic covariant and contravariant
Version 3.0
        Released on: November, 2007         Target Framework: 2.0, 3.0 and 3.5         Visual Studio version: 2008 and 2010
  • Auto-implemented properties
  • Anonymous types
  • Query expressions
  • Lambda expressions
  • Expression trees
  • Extension methods
  • Implicitly typed variables
  • Partial methods
  • Object and collection initializers
Version 2.0
        Released on: November, 2005         Target Framework: 2.0         Visual Studio version: 2005
  • Generics
  • Partial types
  • Anonymous methods
  • Nullable types
  • Iterators
  • Covariance and contravariance
Version 1.0
        Released on: January, 2002         Target Framework: 1.0         Visual Studio version: 2002
  • Classes
  • Stucts
  • Interfaces
  • Events
  • Properties
  • Delegates
  • Expressions
  • Statements
  • Attributes