C# Notes
Learning Notes These are notes I’m making while learning about something. They are not comprehensive and may be limited in their usefulness.
Style
Member names are PascalCase, which is wild.
decimal types
128-bit decimal types! These are really cool! Literals have an M suffix: 1.1M.
Checking for null
I’ve seen it suggested to use pattern matching: foo is not null. == can be overridden.
You can also use foo is { } which will generate a compile-time error if foo’s type is not nullable, which is nice, but goodness that syntax.
However! Maybe what you really want is the null-conditional operator: Nullable?.Foo().
Null coalescing
nullable ?? otherValue
Array sizes
Array lengths are fixed. Clear(array) sets all values to 0-ish and does not change the length. (There is a Resize<T>(array) method but it only works on one-dimensional arrays.)
When creating a new array, values are filled with null or zero-ish values.
Multi-dimensional arrays and arrays-of-arrays
int[,] a = new int[4, 5]
a.Length // 20
a.Rank // 2
a.GetLength(0) // 4
The type is the base type and dimensions, but not the length of those dimensions. int[] is distinct from int[,].
Arrays-of-arrays, but these can be jagged: foo[3][3]. foo.Length would be 3.
Array index from end
^n counts from the end; ^1 is the index of the last item. There is no element ^0, but you might use that with ranges.
Public get, private set
public Type Name { get; private set; }
readonly isn’t like Objective-C’s readonly; in C# it means the field is only assignable in the declaration or constructor.