Skip to content
Bassam Alugili edited this page Jan 27, 2017 · 11 revisions

#Welcome to the CSharp7Features wiki!

C# 7 in Core

Binary Literals & Digits Separators

New langauge Syntax to improve readability for numeric constants.

int binary = 0b1001_1010_1001_0100;
int hex = 0x1c_a0_41_fe;
double real = 1_00.99_9e-1_000;

Out variables

Declare out variablie inline.

public static void OutVarTest(string @int)
{
  int.TryParse(@int, out int t);
  Console.WriteLine(t);
}

Local Functions

Nesting functions inside other functions to limit their scope and visibility.

  • Better performance

  • Reduced memory pressure

    public static void BasicExample() { // Lambda Function Action lambdaFun = () => Console.WriteLine("I'm Lambda"); lambdaFun.Invoke(); // Local Functions Basic void EmptyLocalFunction() { Console.WriteLine("I'm Local"); }; EmptyLocalFunction(); }


Ref Returns

var array = new[] { 1, 2, 3, 4, 5};
ref int GetItem(int[] arrayParam, int index) => ref arrayParam[index];
ref int item = ref GetItem(array, 1);

Throw expressions

You can throw exceptions in code constructs that previously were not allowed because throw was a statement.

  • Null coalescing expressions

  • Some lambda expressions

  • Expression-bodied

    public static double Divide(int x, int y) { return y != 0 ? x % y : throw new DivideByZeroException(); } var a = Divide(10, 0);


Expression-bodied

C # 6 expression-bodied members expanded

  • Constructors
  • Finalizers
  • Indexers See the code example!

Generalized async return

Define custom return types on async methods

  • ValueTask
  • Designed for the very scenario

Tuples

A tuple is a data structure that has a specific number and sequence of Elements.

  • ValueTuple
  • Immutable

Destruction

Destruct an object to a tuple


Pattern Matching

Pattern matching (similar to a switch statement that works on the “shape” of the data, for example its type, rather than just the value)

  • Constant patterns
  • Type patterns
  • Var patterns