C# 9 came out in 2020. Here are some of my favorite additions, and some least favorites
Favorite additions
I can't say there was a lot of things I liked in the C# 9 addition. But here are a few:
Records
Many of the coding projects I work on have basic "data" objects: Classes with nothing but public properties, and used to pass data between modules or architectural layers. The new Record types feature of C# 9 will prove helpful for these scenarios. Here's a simple example:
public record Person { public string FirstName { get; set; } public string LastName { get; set; } };
As far as the details on record usage, I would suggest reading more on them here.
Target-typed new expressions
The var
keyword was added back in C# 3, eliminating the need for specifying the type of variable you are creating, knowing that the compiler could figure it out:
var newPerson = new Person("My name"); var owner = OwnerOfById(theId);
Side note: Personally I don't always like the use of var
. I like clear, easy to understand code. In the second line above, although the compiler knows the return type, coders looking at owner
don't necessarily know. Putting the actual type instead of var
makes it more readable.
With C# 9, you can reduce code when instantiating classes by skipping the name of the class after new
if it's defined at the beginning of the line, as in:
Person newPerson = new("My name"); Person anotherPerson = new() { Name = "My name", Age = 12 };
Target-typed conditional expressions
Prior to C# 9, the ternary operator (?:) required both conditionals to be of the same type. But now you can have them be different values as long as they have the same base type:
Vehicle myVehicle = IsSoccerMom ? new MommyVan() : new Motorcycle();
Pattern matching (the good)
I'm putting this one in both the liked and disliked category (see below).
C# 9 adds some slick pattern matching, allowing you to write code such as:
// Notice the use of keywords "and" and "or" below, and that c is referred to only once: public static bool IsLetter(this char c) => c is >= 'a' and <= 'z' or >= 'A' and <= 'Z'; // is not null: if (a is not null) {...}
Not so favorite additions
Ok, so here are a few additions I'm not so fond of:
Switch pattern matching (the not-so-good)
Programmers can now write fancy code like this:
Enumerable.Range(1, limit) .Select(i => (i % 3 == 0, i % 5 == 0, i) switch { (true, false, _) => "Fizz", (false, true, _) => "Buzz", (true, true, _) => "FizzBuzz", (_, _, var n) => $"{n}" }) .ToList() .ForEach(Console.WriteLine);
This code is concise, and fancy...but hard to read.
There are a few examples like these, which people have actually given to highlight this new 'feature'. Personally, I like readable code. Readability and maintainability are important aspects of source code. I know new C# features can make code shorter, but I don't think shorter is always better.
Top-level statements
So apparently now you can write a program like this one:
using System; Console.WriteLine("Hello World!");
and not have to implement a static Main().
For me "the jury's still out" on this. I'm noticing the C# design team is creating shorter and shorter code. Yes, that can be nice when coding in a hurry. But as I mentioned above, I'm more interested in good readability and maintainability. Before this, it was nice to be able to search on "Main()". Now there's no "Main()" to search on. I'll just have to give this one some time before I consider this a big benefit...