-
C# 12 features: Inline arrays
Part 6 in a series on new language features in C# 12.
Inline arrays
Super-niche, but I guess if you need it, you'll appreciate it. Previously you'd probably need to resort to using
unsafe
code to deal with this.[System.Runtime.CompilerServices.InlineArray(10)] public struct InlineArray { public int Thing; }
The compiler now knows this is an array of 10 contiguous elements.
Further reading
Example source
https://github.com/flcdrg/csharp-12/blob/main/06-inline-arrays/InlineArrays.cs
-
C# 12 features: Using aliases
Part 5 in a series on new language features in C# 12.
Alias any type
Now you can create aliases for any type - it doesn't need to have a name.
Tuple
s are a great example, but also arrays.using P = Tuple<string, string, int>; using C = char[];
Further reading
Example source
https://github.com/flcdrg/csharp-12/blob/main/05-alias/Aliases.cs
-
C# 12 features: Default lambda parameters
Part 4 in a series on new language features in C# 12.
Default lambda parameters
A pretty simple addition to the language - you can now provide default values for lambda expression parameters.
var IncrementBy = (int source, int increment = 1) => source + increment;
In the lambda defined above, if you don't supply the
increment
parameter, it defaults to1
.Again, interesting, but I've not come across a need to use this yet.
Further reading
Example source
https://github.com/flcdrg/csharp-12/blob/main/04-optional-params-lambda/LambdaParameters.cs