This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

C# 7.0 Language Features Cheat Sheet

A quick reference guide for C# 7.0: out variables, pattern matching, tuples and deconstruction, local functions, ref returns, digit separators, and expression-bodied members.

· Published

A quick syntax reference for features introduced in C# 7.0 (.NET Framework 4.7 / .NET Core 2.0).

out Variables

Declare output variables directly inline within method call parameter lists:

// Before C# 7:
int result;
if (int.TryParse(input, out result)) { ... }

// In C# 7:
if (int.TryParse(input, out int result))
{
    Console.WriteLine($"Parsed number: {result}");
}

// Discarding unneeded out parameters with _
if (int.TryParse(input, out _))
{
    Console.WriteLine("Input is valid numeric");
}

Pattern Matching

Type Patterns with is

public void PrintArea(object shape)
{
    if (shape is Circle c)
    {
        Console.WriteLine($"Circle area: {Math.PI * c.Radius * c.Radius}");
    }
    else if (shape is Rectangle r)
    {
        Console.WriteLine($"Rectangle area: {r.Width * r.Height}");
    }
}

Switch Statements with Patterns & when Clauses

switch (shape)
{
    case Circle c:
        Console.WriteLine($"Circle with radius {c.Radius}");
        break;
    case Rectangle r when r.Width == r.Height:
        Console.WriteLine($"Square {r.Width} x {r.Height}");
        break;
    case Rectangle r:
        Console.WriteLine($"Rectangle {r.Width} x {r.Height}");
        break;
    case null:
        throw new ArgumentNullException(nameof(shape));
    default:
        Console.WriteLine("Unknown shape");
        break;
}

Tuples & Deconstruction

Requires System.ValueTuple package in older targets:

Named Tuple Returns

(string First, string Last, int Age) GetUser()
{
    return ("Alice", "Smith", 30);
}

var user = GetUser();
Console.WriteLine($"{user.First} is {user.Age} years old");

Tuple Deconstruction

// Deconstruct into individual variables
var (first, last, age) = GetUser();

// Or onto existing variables
string f, l;
int a;
(f, l, a) = GetUser();

Local Functions

Declare helper methods nested inside parent function scopes:

public int Factorial(int n)
{
    if (n < 0) throw new ArgumentException("Negative value", nameof(n));
    return Calculate(n);

    int Calculate(int x)
    {
        return x <= 1 ? 1 : x * Calculate(x - 1);
    }
}

Literal Enhancements

// Digit separators (_)
int million = 1_000_000;
long hexMask = 0xFF_EC_00_12;

// Binary literals (0b)
byte byteFlag = 0b1010_0110;

ref Locals and Returns

Return references to storage locations rather than copied values:

public ref int Find(int target, int[] numbers)
{
    for (int i = 0; i < numbers.Length; i++)
    {
        if (numbers[i] == target) return ref numbers[i];
    }
    throw new IndexOutOfRangeException();
}

int[] array = { 10, 20, 30 };
ref int val = ref Find(20, array);
val = 99; // array[1] becomes 99

Expression-Bodied Members

Expanded expression-bodied syntax to constructors, finalizers, and property getters/setters:

class Item
{
    private string _name;

    // Expression-bodied constructor
    public Item(string name) => _name = name ?? throw new ArgumentNullException(nameof(name));

    // Expression-bodied property accessor
    public string Name
    {
        get => _name;
        set => _name = value ?? "default";
    }
}

Related topics