c# 8 - new constructs





Nullable reference type 

String and other reference type can be assigned null value.

string? s = null; // Ok

New Switch statement 

I really like the switch statement. This is more compact


var area = figure switch { Line _ => 0, Rectangle r => r.Width * r.Height, Circle c => Math.PI * c.Radius * c.Radius, _ => throw new UnknownFigureException(figure) };


Async streams


Working with streams of data used to be like this :-

async Task<int> GetBigResultAsync() { var result = await GetResultAsync(); if (result > 20) return result; else return -1; }


Now, it supports full async


async IAsyncEnumerable<int> GetBigResultsAsync() { await foreach (var result in GetResultsAsync()) { if (result > 20) yield return result; } }


Range (..) and from end operator ^


Now we can write

int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
i1 = 3// from end operator ^ at work counting from the back of our array.i2 = ^4var slice = a[i1..i2]; // { 3, 4, 5 }
Interface with default implementation

Sounds like mixing in an abstract class to me but this new feature allow us to provide default implementations

interface ILogger { void Log(LogLevel level, string message); void Log(Exception ex) => Log(LogLevel.Error, ex.ToString()); // New overload }


A link here says indexer is supported but i can't get it to run on my VS2017 with Net Framework 2.1




Comments

Popular posts from this blog

gemini cli getting file not defined error

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

vllm : Failed to infer device type