Hacker News new | ask | show | jobs
by starik36 1483 days ago
I love it, but then I've been doing C# since the beginning. The language does appear to show its age though. I recently tried teaching it to my kid and realized that it has grown quite a bit of cruft.

There are a ridiculous amounts of ways to declare a variable.

    Employee employee = new Employee();
    var employee = new Employee();
    Employee employee = new();
And that doesn't even count the initializers {}, records, dynamic, etc... All this makes it difficult for a new develop to jump into it.
2 comments

The first two are not necessarily equivalent. If your class implements an interface you can type the variable as this interface and have different behavior if the interface methods were declared explicitly [1]. You can also use it to interact with for-each loops and some legacy (2.0 and prior) APIs and save yourself an explicit cast.

The last one is a very recently added shorthand which, honestly, I think was just implemented because an intern needed a feature on master to get hired or something like that... I don't get it either.

1: https://docs.microsoft.com/en-us/dotnet/csharp/programming-g...

The last one is great for field initializers where you can't use var.
The first way is no longer recommended. You are encouraged to only use the latter two for readability (except for the interface object type situation mentioned in the other thread).

> The language does appear to show its age though.

Come to Java land. It takes decades to add a single feature. Grass is always greener.