Вы находитесь на странице: 1из 2

C# 8 Feature Cheat Sheet

Default interface methods Pattern matching enhancements Using declarations


Allows you to add new functionality to your Provides the ability to deconstruct matched Enhances the ‘using’ operator to use with
interfaces of your libraries and ensure the objects, and giving you access to parts of their Patterns and make it more natural.
backward compatibility with code written for data structures. C# offers a rich set of patterns
older versions of those interfaces. that can be used for matching: using var repository = new Repository();
Console.WriteLine(repository.First());
// repository is disposed here!
interface IWriteLine • Switch expressions
{ • Property patterns
public void WriteLine()
• Tuple patterns Enhancement of interpolated
{
Console.WriteLine("Wow C# 8!"); • Positional patterns verbatim strings
} Allows @$"" as a verbatim interpolated string,
static bool Positive(Point p) => p switch
} { var file = @$"c:\temp\{filename}";//C# 8
(0, 0) => true,
(var x, var y) when x > 0 && y > 0 => true,
_ => false Null-coalescing assignment
Nullable reference types };
Simplifies a common coding pattern where a
Emits a compiler warning or error if a variable variable is assigned a value if it is null.
that must not be null is assigned to null. It is common to see the code of the form:
Asynchronous streams
string? nullableString = null; Allows to have enumerators that support async if (variable == null)
// WARNING: may be null! Take care! operations. {
Console.WriteLine(nullableString.Length) variable = expression; // C# 1..7
await foreach (var x in enumerable) }
{
Console.WriteLine(x); variable ??= expression; // C# 8
}
Static local functions Unmanaged constructed types Stackalloc in nested expressions
Allows you to add the 'static' modifier to the Allows you to take a pointer to unmanaged The result of a stackalloc expression is of the
local functions. constructed types, such as ValueTuple<int, int>, System.Span<T> or System.ReadOnlySpan<T>
as long as all the elements of the generic type type.
int AddFiveAndSeven() are unmanaged. Span<int> set = stackalloc[] { 1, 2, 3,
{ 4, 5, 6 };
int y = 1; int x = 2; struct Foo<T> where T : unmanaged var subSet = set.Slice(3, 2);
return Add(x, y); { foreach (var n in subSet)
} Console.WriteLine(n); // Output: 4 5
static int Add(int o, int t) => o + t;
} public unsafe void Test()
{ Disposable ref structs
var foo = new Foo<int>();
Allows you to use the ‘using’ pattern with ref
var bar = &foo; // C# 8
Indices and ranges } struct or readonly ref struct‘.
Allows you to use more natural syntax for // Pattern-based using for ref struct
specifying subranges in an array or a collection. Readonly-Member ref struct Test {
public void Dispose() {}
int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Allows you to apply the readonly modifier to }
any member of a struct.
Index: Used to obtain the collection from the using var test = new Test();
beginning or from the end. public struct XValue // test is disposed here!
{s
// Number 4 from end of the collection private int X { get; set; }
Index i2 = ^4; public readonly int IncreaseX() About me
Console.WriteLine($"{a[i2]}"); // "6" {
// This will not compile: C# 8 Bassam Alugili
X = X + 1; CSharpCorner
Range: Access a sub-collection(slice) from a
www.bassam.ml
collection. var newX = X + 1; // OK 25.10.2019
return newX;
var slice = a[i1..i2]; // { 3, 4, 5 } } Powerful Feature
}

Вам также может понравиться