To me, the benefit of "Target-typed new expressions" is more with bringing consistency to class-scope member declarations and method-scope member declarations, so these two can look the same, and a programmer only has to read one way of declaring new members in C#: public class MyClass { private readonly MyDependency _myDependency = new(); public void MyMethodThatDoesWork() { MyVariable variable = new(); } } As oppos…
I can't see var going anywhere, many times you aren't directly creating a new object, but getting a generated object from something ( like linq) which tends to have more complicated type signatures.
C# 9 top-level programs and target-typed expressions
11–20 of 188 posts
Re: C# 9 top-level programs and target-typed expressions
#12I think a better justification is that fields and properties does not support type inference, so this can avoid some redundant type expressions for initializes, just like "var" can avoid redundant type expressions for local variables.
Of course it would be more elegant if fields/properties supported type inference like "var", but that is a can of worms since you can have recursive dependencies.
Re: C# 9 top-level programs and target-typed expressions
#13- high performance is important - or interop with native libraries is required (because C#'s DllImport attribute makes that super simple)
Another benefit is that C# is syntactically similar to TypeScript (they were both designed by Anders Hejlsberg after all), so switching between the two languages feels easy. Java and Go are both similar to C# in terms of performance, but interfacing with native code isn't as simple with those languages, and they're also not as similar to TypeScript as C# is (especially Go, which is quite different).
Re: C# 9 top-level programs and target-typed expressions
#14Unfortunately the new tricks can't be used when the codebase is still stuck on .net framework :(
Allegedly there's an upgrade path from .NET 4.8 to .NET 5.
MS really made a grave mistake by coupling the evolution of the C# language to the .NET version. This means all projects on the .NET framework is now also stuck with an obsolete language version for no good reason.
It is becoming a a lot less fun to be a C# developer because you more often will have to work with obsolete tools.
Re: C# 9 top-level programs and target-typed expressions
#15I think that C# is underrated. Although TypeScript is my default language choice for most programming, C# / .NET is my choice for cases where: - high performance is important - or interop with native libraries is required (because C#'s DllImport attribute makes that super simple) Another benefit is that C# is syntactically similar to TypeScript (they were both designed by Anders Hejlsberg after all), so switching bet…
Re: C# 9 top-level programs and target-typed expressions
#16Earlier quoted context omitted.
Allegedly there's an upgrade path from .NET 4.8 to .NET 5.
if you have built anything on web forms, then you really have no way forward other than rewriting all that. That alone will keep many an enterprise locked on .NET Framework for quite some time.
Re: C# 9 top-level programs and target-typed expressions
#17Unfortunately the new tricks can't be used when the codebase is still stuck on .net framework :(
Obviously, it's not suggested doing that nor is entirely well supported, but it is at least allowed.
Though if you are looking to use top-level programs you probably are working in greenfields anyway that should just be .NET 5+. It's not really a feature that makes a lot of sense for existing codebases as it is more a teaching tool/"quick and dirty command line app/script" tool.
Re: C# 9 top-level programs and target-typed expressions
#18Who would have believed a decade ago that there would be a RedHat developer blog entry on C# today? If someone had told you that then you would have thought the world would have collapsed in the meantime.
Re: C# 9 top-level programs and target-typed expressions
#19It is becoming way to loose in a sense and it seems that it wants more than it should.
If you want to do functional programming, pick a fp language.
If you want to do dynamic typed programming pick a dp language.
Sure you can mix some things in, but c# is becoming way to scattered imo. And it's not pretty i think.
Re: C# 9 top-level programs and target-typed expressions
#20I think that C# is underrated. Although TypeScript is my default language choice for most programming, C# / .NET is my choice for cases where: - high performance is important - or interop with native libraries is required (because C#'s DllImport attribute makes that super simple) Another benefit is that C# is syntactically similar to TypeScript (they were both designed by Anders Hejlsberg after all), so switching bet…
C# is my goto for Windows, for sure. Is the native interop good in Linux as well? I've never even considered anything like that, would probably gravitate to C++ since that's what I'm familiar with but would seriously consider Go or Java in non-Windows situations.