Live data from Hacker News

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

developers.redhat.com

41–50 of 188 posts

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

#41

Earlier quoted context omitted.

A bit of a tangent but is it possible yet to compile C# to native code easily (like calling a normal compiler on the command-line)? Last time I checked I needed to jump through a bunch of hoops through Visual Studio, and create some XML or other nonsense. I don't get why they've made it so hard compared to just compiling managed code.

It is super easy with dotnet core. The new CLI is pretty simple, you use it as a package manager and compiler. Visual Studio also uses the CLI to run and debug dotnet core projects. https://docs.microsoft.com/en-us/dotnet/core/tools/

Is it possible to create a single, stand-alone, self-contained .exe? (I'm fine with only building for a single platform - Mac/Win/Linux)

Needing to run my programs (which show up as .dll's) using dotnet just feels weird (and it doesn't match my intuition for 'how programs are run' in Windows cmd/etc).

I'd be fine with an .exe that's not self contained but at least I run it like a 'normal' exe. :)

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

#42

Earlier quoted context omitted.

It is super easy with dotnet core. The new CLI is pretty simple, you use it as a package manager and compiler. Visual Studio also uses the CLI to run and debug dotnet core projects. https://docs.microsoft.com/en-us/dotnet/core/tools/

Is it possible to create a single, stand-alone, self-contained .exe? (I'm fine with only building for a single platform - Mac/Win/Linux) Needing to run my programs (which show up as .dll's) using dotnet just feels weird (and it doesn't match my intuition for 'how programs are run' in Windows cmd/etc). I'd be fine with an .exe that's not self contained but at least I run it like a 'normal' exe. :)

> Is it possible to create a single, stand-alone, self-contained .exe?

Yes.

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

#43
post #39

Earlier quoted context omitted.

Agreed that `var` isn't going anywhere, but this feature's best use case is with complex generics/enumerables and things like anonymous tuple types. For example: // old: var x = new List > { new KeyValuePair ("a", 1), new KeyValuePair ("b", 2) }; // new var x = new List > { new("a", 1), new("b", 2) };

Wow, when I (only a C# dabbler) read about this feature in the article I thought it sounded incredibly silly, but this really sells it. Was there really no better way to create tuples before?

You could create tuples with `Tuple.Create("a", 1)` since at least .NET 4, if not 3.

Since C# 7 you can also create tuples using just `("a", 1)`. But tuples are not KeyValuePairs. So the new "new" syntax will be very helpful in a lot of cases.

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

#44

Earlier quoted context omitted.

It is super easy with dotnet core. The new CLI is pretty simple, you use it as a package manager and compiler. Visual Studio also uses the CLI to run and debug dotnet core projects. https://docs.microsoft.com/en-us/dotnet/core/tools/

Is it possible to create a single, stand-alone, self-contained .exe? (I'm fine with only building for a single platform - Mac/Win/Linux) Needing to run my programs (which show up as .dll's) using dotnet just feels weird (and it doesn't match my intuition for 'how programs are run' in Windows cmd/etc). I'd be fine with an .exe that's not self contained but at least I run it like a 'normal' exe. :)

You have to set up "dotnet publish" for the project that makes the executable.

It then makes a stub loader "your program.exe" which looks and behaves normally. You can then make it "self contained", which bundles the assemblies for you and transparently unpacks them.

You can have different publish profiles for the different targets.

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

#45
post #39

Earlier quoted context omitted.

Agreed that `var` isn't going anywhere, but this feature's best use case is with complex generics/enumerables and things like anonymous tuple types. For example: // old: var x = new List > { new KeyValuePair ("a", 1), new KeyValuePair ("b", 2) }; // new var x = new List > { new("a", 1), new("b", 2) };

Wow, when I (only a C# dabbler) read about this feature in the article I thought it sounded incredibly silly, but this really sells it. Was there really no better way to create tuples before?

[deleted]

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

#46

Earlier quoted context omitted.

It is super easy with dotnet core. The new CLI is pretty simple, you use it as a package manager and compiler. Visual Studio also uses the CLI to run and debug dotnet core projects. https://docs.microsoft.com/en-us/dotnet/core/tools/

Is it possible to create a single, stand-alone, self-contained .exe? (I'm fine with only building for a single platform - Mac/Win/Linux) Needing to run my programs (which show up as .dll's) using dotnet just feels weird (and it doesn't match my intuition for 'how programs are run' in Windows cmd/etc). I'd be fine with an .exe that's not self contained but at least I run it like a 'normal' exe. :)

Yes that is possible, you can produce an .exe which contains your application's assemblies but relies on the appropriate .NET runtime being present on the user's computer. You can also build a .exe which has the .NET runtime bundled, but it can be quite big. I made a simple program that wrote "Hello World" to the console [1] and it was between 46-78MB: https://blog.mclemon.org/no-this-is-what-peak-hello-world-lo...

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

#47
post #39

Earlier quoted context omitted.

Agreed that `var` isn't going anywhere, but this feature's best use case is with complex generics/enumerables and things like anonymous tuple types. For example: // old: var x = new List > { new KeyValuePair ("a", 1), new KeyValuePair ("b", 2) }; // new var x = new List > { new("a", 1), new("b", 2) };

Wow, when I (only a C# dabbler) read about this feature in the article I thought it sounded incredibly silly, but this really sells it. Was there really no better way to create tuples before?

C# (as of 7.0) has pretty nice tuple syntax that supports all things you expect (deconstruction, etc):

https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...

But the situation here is that the type KeyValuePair is older than that and isn't a tuple.

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

#48
post #10

Earlier quoted context omitted.

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.

I hate hate hate var. Sure, in your own IDE, and reading the code you just wrote, no problem. But when doing code reviews and jumping all over, I want to see the type right there. Nothing more frustrating than reviewing a PR with var's all over. This isn't even up for debate anymore... No more var! It is frustrating to still see var as the recommended way by Microsoft... You can even put in a rule to format the docum…

Can't disagree more. var improves readability so much that I can't imagine reviewing code anymore with unnecessary type declarations everywhere.

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

#49
post #46

Earlier quoted context omitted.

Is it possible to create a single, stand-alone, self-contained .exe? (I'm fine with only building for a single platform - Mac/Win/Linux) Needing to run my programs (which show up as .dll's) using dotnet just feels weird (and it doesn't match my intuition for 'how programs are run' in Windows cmd/etc). I'd be fine with an .exe that's not self contained but at least I run it like a 'normal' exe. :)

Yes that is possible, you can produce an .exe which contains your application's assemblies but relies on the appropriate .NET runtime being present on the user's computer. You can also build a .exe which has the .NET runtime bundled, but it can be quite big. I made a simple program that wrote "Hello World" to the console [1] and it was between 46-78MB: https://blog.mclemon.org/no-this-is-what-peak-hello-world-lo...

Yeah, it's not really a "real" native binary.

You can get it down to 8kb by jettisoning all comforts: https://medium.com/@MStrehovsky/building-a-self-contained-ga... but that's not exactly practical.

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

#50
post #14

Earlier quoted context omitted.

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 tool…

> MS really made a grave mistake by coupling the evolution of the C# language to the .NET version.

For the most part that isn't true. You can use later C# compilers with older frameworks. Some features do, unavoidably, require framework support.

But that being said, the changes in C# aren't often that radical that I feel particularly constrained by older versions.

Post reply on HN