CSharp for the second time.

I last did CSharp 11 years ago. So, I have forgotten most of it. I notice loads of features in common with Scala, Java, Rust, and so on, but that doesn’t change the fact that the syntax is unique and these notes are my memory joggers.

Arrays

string [] Days = {"M", "T", "W", "T", "F", "S", "S"};
var Days1 = new[]{"M", "T", "W", "T", "F", "S", "S"};

A jagged array has arrays within it of differing sizes

int [][] arrays = new int[3][] {
  new [] {1,2},
  new [] {1,2,3},
  new [] {1},
}

A rectangular array has a different way to declare it

int [,]rect = new int[5,7];
var anotherRect = new int[,]
{
  {1,2},
  {3,4},
}

Index from end operator

You can use ^0 to mean the length, ^1 to mean the last item. Its an operator.

var array = new int[]{1,2,3};
var third = array[2];
var last = array[^1];

Range operator

var array = new int[]{1,2,3,4,5};
var slice1 = array[2..^3]; // array[new Range(2, new Index(3, fromEnd:true))];
var slice2 = array[..^3];  // array[Range.EndAt(new Index(3, fromEnd:true))];
var slice3 = array[2..];   // array[Range.StartAt(2)];
var slice4 = array[..];    // array[Range.All];

There is even an Index type which can hold the end relative index values

Index last = ^1;

Iterator via yield

public static IEnumerable<int> Countdown(int start, int end)
{
  for (int i = start; i >= end; --i)
  {
    yield return i;  // You can see this is generating the IEnumerable
  }
}

Tuples

ValueTuple use structs (ie values) and are the default when you declare a tuple.

There is an older form of just Tuple which are all classes.

Returning a tuple

public static (int x, int y) Pos() => (10,20);

Dictionary

var monLookup = new Dictionary<string, int>
{
  {"Jan", 1},
  {"Feb", 2},
}