Tutorial by Examples

A common pattern in C# is using bool TryParse(object input, out object value) to safely parse objects. The out var declaration is a simple feature to improve readability. It allows a variable to be declared at the same time that is it passed as an out parameter. A variable declared this way is sco...
The 0b prefix can be used to represent Binary literals. Binary literals allow constructing numbers from zeroes and ones, which makes seeing which bits are set in the binary representation of a number much easier. This can be useful for working with binary flags. The following are equivalent ways o...
The underscore _ may be used as a digit separator. Being able to group digits in large numeric literals has a significant impact on readability. The underscore may occur anywhere in a numeric literal except as noted below. Different groupings may make sense in different scenarios or with different ...
Basics A tuple is an ordered, finite list of elements. Tuples are commonly used in programming as a means to work with one single entity collectively instead of individually working with each of the tuple's elements, and to represent individual rows (ie. "records") in a relational databa...
Local functions are defined within a method and aren't available outside of it. They have access to all local variables and support iterators, async/await and lambda syntax. This way, repetitions specific to a function can be functionalized without crowding the class. As a side effect, this improves...
Pattern matching extensions for C# enable many of the benefits of pattern matching from functional languages, but in a way that smoothly integrates with the feel of the underlying language switch expression Pattern matching extends the switch statement to switch on types: class Geometry {} cl...
Ref returns and ref locals are useful for manipulating and returning references to blocks of memory instead of copying memory without resorting to unsafe pointers. Ref Return public static ref TValue Choose<TValue>( Func<bool> condition, ref TValue left, ref TValue right) { ...
C# 7.0 allows throwing as an expression in certain places: class Person { public string Name { get; } public Person(string name) => Name = name ?? throw new ArgumentNullException(nameof(name)); public string GetFirstName() { var parts = Name.Split(' '); ...
C# 7.0 adds accessors, constructors and finalizers to the list of things that can have expression bodies: class Person { private static ConcurrentDictionary<int, string> names = new ConcurrentDictionary<int, string>(); private int id = GetId(); public Person(string n...
Task<T> is a class and causes the unnecessary overhead of its allocation when the result is immediately available. ValueTask<T> is a structure and has been introduced to prevent the allocation of a Task object in case the result of the async operation is already available at the time of...

Page 1 of 1