Trying to upgrade my C# chops this year but was curious which features and version of C# does the latest Unity currently support? Is there a list somewhere with all the C# features?
Basically, I’d just go look up about what’s new in C# 6.0. For example:
Of these, I find string interpolation to be the most useful on a day-to-day basis. So:
// Instead of this:
var s = string.Format("{0}, {1}, {2}", arg0, arg1, arg2);
// You can now do this:
var s = $"{arg0}, {arg1}, {arg2}";
It doesn’t sound like much, but it saves a lot of hassle when modifying an expression, and avoids cases like this where you used the wrong args:
// Oops: This causes an exception because you forgot the 3rd argument.
var s = string.Format("{0}, {1}, {2}", arg0, arg1);
The null-conditional is pretty nice, and can cut down on the verbosity of your code. So:
// Instead of this:
int x;
if (a != null && a.b != null && a.b.c != null) {
x = a.b.c.d;
}
// You can now do this:
int x = a?.b?.c?.d;
The “?” null-conditional operator automatically short circuits if it hits a null, instead of giving you a null reference exception.
The new ‘nameof’ is also pretty useful when generating messages related to a specific variable.