Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simpler way to get the "old style" console templates #3985

Closed
dotMorten opened this issue Oct 6, 2021 · 18 comments
Closed

Add simpler way to get the "old style" console templates #3985

dotMorten opened this issue Oct 6, 2021 · 18 comments
Assignees
Labels
area: template-content The issue is related to content of template packages managed in this repo (/template_feed) gathering-feedback The issue requires feedback in order to be planned, please comment if the feature is useful for you need-pm-discussion Need agreement from PM that the issue aligns to targeted stories for any of the next 2 releases

Comments

@dotMorten
Copy link

If I don't want to use the new console style projects with generated top-level statements , since I might feel it becomes the odd-one-out file in a project otherwise consisting of "full" classes, I have to do so in a very round about way, as documented here:
https://docs.microsoft.com/en-us/dotnet/core/tutorials/top-level-templates#use-the-old-program-style
image

The problem with this approach (apart from being very unintuitive) is that it doesn't work from within VS2022 (sure I could pick 5 then manually change it after, but if I don't have the 5 SDK installed that leads to more confusing errors on project creation).

Instead I think it would have been cleaner if there was a boolean I could set that the VS IDE also allows me to uncheck to disable the use of top level statements .
It would have been cleaner to have something like

    dotnet new console --useTopLevelStatements false

and in VS this would translate to a checkbox I can uncheck.

@glennsills
Copy link

How often to you "dotnet new console"? As features go this one has minimal utility.

@dotMorten
Copy link
Author

dotMorten commented Oct 6, 2021

@glennsills VERY often in VS (not so much from the console). The suggestion has zero effect on everyone who wants the new template, as it's still default. But it does give an easy opt-out for those who doesn't want it.

@jonmikeli
Copy link

Adding to the initial @dotMorten 's request, it would be great whether we could set the default value too (in order to avoid having to type the same options all the time).

Answering to @glennsills, in the team, we use console applications quite often for different types of tests, simulators, etc (mainly in IoT projects, which I understand are quite specific, but not exclusively). We are even working on our own templates for this.

@sonnemaf
Copy link

sonnemaf commented Oct 7, 2021

How often to you "dotnet new console"? As features go this one has minimal utility.

In a C# training I do this maybe 10 times a day. So I do this 50 times a month.

@KathleenDollard
Copy link

An update on this:

Unfortunately the directions in docs are not complete and that approach relies on .NET 5 being installed. We will update the page, but it is more difficult to get old style projects than suggested in the page as posted. Honestly, it will probably be easiest to just copy the old version from docs (which we included in the page for your convenience).

We do not plan to change this for .NET 6 GA. We are still assessing the best path and do not want to make a knee jerk reaction - there's a key question in whether folks hate all four of the changes in the new templates, or just this one:

  • Top level program (no namespace, class or main method)
  • Implicit global usings (no usings at the top for common things)
  • Nullable reference is on by default
  • File scoped namespaces, in the class template (no curlies after the namespace)

There are also questions around naming of a switch. I know one is suggested in this issue, but it's long and most of you have not enabled tab completion, and we'd want more feedback and consideration of the other issues. Even if we were ready to make this change, we do not have time to get feedback to get it right for .NET 6 GA.

We want your feedback and to help with the decision on providing a way to opt in to the old style templates. If haven't voted on the issue, please do.

@dotMorten Could you edit the initial issue to say that we are updating the docs page and that it also requires .NET 5 to be installed. If you rather I do it, let me know.

@sonnemaf Could you give feedback on whether switching training to the new style is something you are considering.

@dotMorten
Copy link
Author

Could you edit the initial issue to say that we are updating the docs page and that it also requires .NET 5 to be installed. If you rather I do it, let me know.

@KathleenDollard I did point out that not having .NET 5 installed creates issues. Don't really get your comment about doc and how that affects this issue. The doc I referred to was just to say "this is how you're telling us to work around it", and wanted to point out that you have use work arounds rather than having a built-in feature. I don't see how updating the doc would change that?

There's a key question in whether folks hate all four of the changes in the new templates, or just this one:

I'd say just the one. Wrt all the other features, those are IMHO completely different and unrelated to this issue - they are also all project settings that can more easily be changed after-the-fact. Enabling new language features seems reasonable when targeting .NET 6 - the issue here and my gut feeling most people don't like is the magic hidden code that gets generated. No other class in your project will look like this, you can't see the namespace, class name, name of parameters or the return type.

I know one is suggested in this issue, but it's long

Just a conceptual thing. I don't care about the final name - In most cases I just want a checkbox generated inside the VS template wizard.

@dotMorten
Copy link
Author

dotMorten commented Oct 9, 2021

I took the existing console template and tweaked it slightly. I was surprised how little I needed to do, because switching it on and off was already there. So here's the experience I got in VS with that (notice the extra checkbox):
image
Most of the other features that @KathleenDollard was asking about are also parameters already, so would be easy to expose the same way.

Uncheck it, and you get this:
image

If you want to try this for yourself, here's the template package:
dotMorten.Console.ProjectTemplates.1.0.1.zip

To install it:
dotnet new --install dotMorten.Console.ProjectTemplates --nuget-source [path to folder with above nuget]
Then open VS2022 and you'll get this experience.
From commandline use:
dotnet new console2 --topLevelProgram=false

You can uninstall with dotnet new --uninstall dotMorten.Console.ProjectTemplates

@sonnemaf
Copy link

@KathleenDollard I don't use the TopLevelStatements because some of the refactorings are not available in it. Take for example the Introduce local.

image

image

Explaining what a class Program and a static method Main is doing isn't that hard. It also gives me the opportunity to explain readonly fields and access modifiers. Maybe it is just me. But I like the old way just better. So no I'm not considering switching to the new style.

@bekir-ozturk bekir-ozturk added the gathering-feedback The issue requires feedback in order to be planned, please comment if the feature is useful for you label Oct 11, 2021
@bekir-ozturk bekir-ozturk added the triaged The issue was evaluated by the triage team, placed on correct area, next action defined. label Oct 11, 2021
@tthiery
Copy link

tthiery commented Nov 2, 2021

Also consider #3654 (and its original https://GitHub.com/dotnet/templates/issues/520 ) proposing a second template main (no global usings, no top level main, rest tbd) as a solution.

@mwwhited
Copy link

I don't use top level statement because the pattern is so disjoin from the rest of the source that it doesn't even seem like the same programming language.

@henrik-jensen
Copy link

henrik-jensen commented Feb 21, 2022

Norville Barnes to his co-dev: "Top-level statements, I developed it my self, yessiree this is my ticket upstairs."
Co-dev: "???"
Norville: "You know,- for (script) kids"
image
😁

Actually,- Norville Barnes idea was genius, a 'Hula hoop', unlike this one, 'Top-level statements'.
Nah,- I prefer my c/c++-style main for my console projects, which also offers a return value

static int Main(string[] args) { return 0; }

Yeah, I know, Environment.Exit(0);, but no!

@vlada-shubina vlada-shubina added the need-pm-discussion Need agreement from PM that the issue aligns to targeted stories for any of the next 2 releases label Mar 11, 2022
@vlada-shubina vlada-shubina removed the triaged The issue was evaluated by the triage team, placed on correct area, next action defined. label Apr 19, 2022
@baronfel
Copy link
Member

I'm happy to report that in .NET SDK 6.0, the --use-program-main option was added to the console and web templates to bring back the 'old style' of console template. This is available in both VS and CLI experiences. Thanks for your engagement and feedback on this issue!

@Nordgaren
Copy link

How often to you "dotnet new console"? As features go this one has minimal utility.

You don't get to decide how people use features. Please don't give non answers like this...

@Nordgaren
Copy link

I am on VS2022 and none of these solutions work, btw. I am sure my SDK is up to date. --use-program-main doesn't work
Error: Invalid option(s):
--use-program-main
'--use-program-main' is not a valid option

and there is no "Top Level Statements" Checkbox when starting a new console app. Unless I need to download a new template? Shouldn't this just be a part of the SDK by now?

I think this new console template is kinda bad. It's just confusing to new users.

@baronfel
Copy link
Member

Can you please show what version of the SDK you're using?

@glennsills
Copy link

glennsills commented Jun 12, 2022

@Nordgaren

Verify this...

PS C:\Users\gbsil\src> dotnet --list-sdks
6.0.300 [C:\Program Files\dotnet\sdk]

If I then run

dotnet new console --use-program-main

Program.cs should then look like:

namespace src;
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");
    }
}

You might also want to verify that there is no global.json forcing the use of an older SDK. You can verify it like this

 dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.300

To get the latest .NET 6 SDK you can

winget install Microsoft.DotNet.SDK.6

Then you can easily update to the latest version when updates come out like this

winget install Microsoft.DotNet.SDK.6

@federico-r-figueredo
Copy link

I am on VS2022 and none of these solutions work, btw. I am sure my SDK is up to date. --use-program-main doesn't work Error: Invalid option(s): --use-program-main '--use-program-main' is not a valid option

and there is no "Top Level Statements" Checkbox when starting a new console app. Unless I need to download a new template? Shouldn't this just be a part of the SDK by now?

I think this new console template is kinda bad. It's just confusing to new users.

I've the exact same problem...

Screenshot from 2022-10-09 10-48-53

@glennsills
Copy link

@federico-r-figueredo Try upgrading you .net sdk to 6.0.300 or greater.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: template-content The issue is related to content of template packages managed in this repo (/template_feed) gathering-feedback The issue requires feedback in order to be planned, please comment if the feature is useful for you need-pm-discussion Need agreement from PM that the issue aligns to targeted stories for any of the next 2 releases
Projects
None yet
Development

No branches or pull requests