Live data from Hacker News

C# 9 top-level programs and target-typed expressions

developers.redhat.com

11–20 of 188 posts

Re: C# 9 top-level programs and target-typed expressions

#11
post #10

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.

Oh true, there are still use cases where it's valuable. `out var ` also comes to mind.

Re: C# 9 top-level programs and target-typed expressions

#12
The article states that the benefit of target-typed new-expressions is that "the type declarations are nicely aligned" compared the use of "var".

I 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
I 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 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

#14
post #2

Unfortunately 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.

The upgrade path is massive rewrites, something most business are wary of. For good reasons.

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

#15

I 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.

Re: C# 9 top-level programs and target-typed expressions

#16
post #6

Earlier 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.

In our case not web forms but WCF

Re: C# 9 top-level programs and target-typed expressions

#17
post #2

Unfortunately the new tricks can't be used when the codebase is still stuck on .net framework :(

Not by default, no. These two specific tricks should work on .NET Framework (some of the other tricks in C# after 7 or 8 require functionality that .NET Framework doesn't have, though). You can ask VS to use the more recent C# compiler with the LangVersion tag in the .csproj: https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...

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

#18

Who 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.

Exactly my first thought. I remember my first steps with C# and it felt very "MS version of Java" and I didn't expect how much a) I would like it and b) how successfully it would spread to other areas

Re: C# 9 top-level programs and target-typed expressions

#19
I have been programming with c# since 2000 i think. But I do not like where the languages is going.

It 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

#20

I 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.

How do you think they built the Linux-version of the .NET standard library? ;)
Post reply on HN