![Dashboard of tools](/assets/posts/2024-07-08-CSharpInconsistencies/Cover.png)
What I Don't Like About C# Evolution: Inconsistency
08 Jul 2024 #csharpC# isn’t just Java anymore.
That might have been true for the early days of C#. But the two languages took different paths.
People making that joke have missed at least the last ten years of C# history.
C# is open source and in constant evolution. In fact, you can upvote and discuss feature proposals in the C# official GitHub repo.
Every .NET release comes with new C# features.
But I’ve stopped obsessing over new C# features. In fact, I stopped collecting my favorite C# features by version. I feel I’m not missing much.
C# is not a consistent language anymore.
We now have many alternatives for the same task. Don’t believe me?
Objects, Arrays, and Null
First, here’s how to create a new object,
Movie m = new Movie("Titanic");
var m = new Movie("Titanic");
Movie m = new("Titanic");
// ^^^^
The last one is the target-typed “new” expressions introduced in C# 9.0. That’s one of the features I disable in a .editorconfig
file.
Second, here’s how to declare and initialize an array,
Movie[] movies = new Movie[] { new Movie("Titanic") };
var movies = new Movie[] { new Movie("Titanic") };
var movies = new Movie[] { new("Titanic") };
// ^^^^^
var movies = new[] { new Movie("Titanic") };
// ^^^^^
Movie[] movies = [ new Movie("Titanic") ];
Movie[] movies = [ new("Titanic") ];
// ^^^
We combine “new” expressions and collection expressions introduced in C# 12.
And, lastly, here’s how to check if an object is not null,
var titanic = new Movie("Titanic")
titanic != null;
titanic is not null;
titanic is {};
titanic is object;
And I don’t want to touch on primary constructors. It’s like classes got jealous of records and started crying for a similar feature, like a baby boy jealous of his brother.
Voilà! That’s what I don’t like about C#. Don’t take me wrong, C# is a great language with excellent tooling. But my favorite features are quite old: LINQ, async/await, and extension methods.
Some new features have lowered the barrier to entry. Now a “Hello, world” is a single line of code: Console.WriteLine("Hello, world!");
.
Other C# features are making the language inconsistent and easier to write but not easier to read.
Often C# doesn’t feel like a single language, but three: the classic one from the early 2000s, another one between the classic and the .NET era, and the one we’re living these days.
Anyway, still no discriminated unions. Maybe in C# 20?
For more C# content, read my C# Definitive Guide, how to avoid exceptions when working with Dictionaries, and Six helpful extension methods to work with Collections.
Happy coding!