-
Notifications
You must be signed in to change notification settings - Fork 991
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
Update C# templates to top level statements #5071
Comments
I would rather the Application.Run() to also be moved to the Initialize method that is source generated and take in the form instance that should be new'd up in that call so it could be simplified to just: // make these global usings in "GlobalUsings.cs".
using System.Threading;
using System.Windows.Forms;
using Company.WinFormsApplication1;
ApplicationConfiguration.Initialize(() => new Form1()); So then in the end the actual Program.cs file could then be chopped down to just a single line of actual code with the Initialize method running that lambda inside when it calls Run for you. |
We won't be changing our templates to use the top level statements (#5071). A typical Windows Forms app consist of multiple types split across multiple files (e.g. Form1.cs and Form1.Designer.cs) that won't work with top level statements. |
Relates to dotnet#5035 Relates to dotnet#5071
Relates to dotnet#5035 Relates to dotnet#5071
@RussKie Verified this issue on .NET 6.0 RC1 latest build: 6.0.10-rc.1.21417.19 + binaries build from main branch under dotnet/winforms repo, after replace following codes in Program.cs file:
with this code is not recognized as following screenshot, want to confirm with you do we need to do some other actions at here? |
|
You mean preview will show in csproj until .NET 6.0 goes GA, we can verify this issue at then? |
@RussKie We just (today) merged the change that removes the requirement. .NET 6.0 GA will enable V2 generators by default. |
Awesome! 🎉 |
Does this mean it also adds the |
AFAIK there is no such thing as Lines 93 to 95 in 07f6de3
|
Top level statements have been released in .NET 5 and C# 9. There are a number of active internal discussion about updating other templates (e.g. ASP.NET Core) to use the top level statements approach.
As discussed in dotnet/designs#223 (comment) C# Windows Forms apps can be written as top level programs today as well, though
[STAThread]
make it a little awkward. So we can consider updating our application templatewinforms/pkg/Microsoft.Dotnet.WinForms.ProjectTemplates/content/WinFormsApplication-CSharp/Program.cs
Lines 1 to 23 in c76a4a4
...and rewrite it as follows:
When dotnet/designs#223 will be implemented the template can be further simplified, and would look something like this:
And, generally speaking, there's nothing that prevents moving
Thread.CurrentThread.SetApartmentState(...)
calls into the source generatedApplicationConfiguration.Initialize()
method:The text was updated successfully, but these errors were encountered: