Skip to content

Update C# templates to top level statements #5071

Closed

Description

@RussKie

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 template

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Company.WinFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

...and rewrite it as follows:

using System.Threading;
using System.Windows.Forms;
using Company.WinFormsApplication1;

// It is unfortunate but we have to set it to Unknown first.
Thread.CurrentThread.SetApartmentState(ApartmentState.Unknown);
Thread.CurrentThread.SetApartmentState(ApartmentState.STA);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(defaultValue: false);
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);

Application.Run(new Form1());

When dotnet/designs#223 will be implemented the template can be further simplified, and would look something like this:

using System.Threading;
using System.Windows.Forms;
using Company.WinFormsApplication1;

// It is unfortunate but we have to set it to Unknown first.
Thread.CurrentThread.SetApartmentState(ApartmentState.Unknown);
Thread.CurrentThread.SetApartmentState(ApartmentState.STA);

ApplicationConfiguration.Initialize();

Application.Run(new Form1());

And, generally speaking, there's nothing that prevents moving Thread.CurrentThread.SetApartmentState(...) calls into the source generated ApplicationConfiguration.Initialize() method:

using System.Threading;
using System.Windows.Forms;
using Company.WinFormsApplication1;

ApplicationConfiguration.Initialize();

Application.Run(new Form1());

Activity

  1. added
    enhancement
    on Jun 10, 2021
  2. RussKie commented on Jun 10, 2021

    @RussKie
    ContributorAuthor

    image

  3. AraHaan commented on Jun 12, 2021

    @AraHaan
    Member

    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.

  4. added
    priority-1
    on Aug 16, 2021
  5. added this to the 6.0 milestone on Aug 16, 2021
  6. RussKie commented on Aug 25, 2021

    @RussKie
    ContributorAuthor

    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.

  7. ghost removed this from the 6.0 milestone on Aug 25, 2021
  8. added and removed
    priority-1
    on Aug 25, 2021
  9. Amy-Li03 commented on Aug 26, 2021

    @Amy-Li03
    Contributor

    @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:

    Application.SetHighDpiMode(HighDpiMode.SystemAware);
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    

    with
    ApplicationConfiguration.Initialize();

    this code is not recognized as following screenshot, want to confirm with you do we need to do some other actions at here?
    image

  10. RussKie commented on Aug 26, 2021

    @RussKie
    ContributorAuthor

    <LangVersion>preview</LangVersion> in csproj until .NET 6.0 goes GA.

  11. Amy-Li03 commented on Aug 26, 2021

    @Amy-Li03
    Contributor

    You mean preview will show in csproj until .NET 6.0 goes GA, we can verify this issue at then?

  12. RussKie commented on Aug 26, 2021

    @RussKie
    ContributorAuthor

    We use Source Generators API v2, which are behind the "preview" lang version. @chsienki @sharwell the langversion=preview won't be require when .NET 6.0 GAs, right?

  13. chsienki commented on Aug 27, 2021

    @chsienki
    Member

    @RussKie We just (today) merged the change that removes the requirement. .NET 6.0 GA will enable V2 generators by default.

  14. RussKie commented on Aug 27, 2021

    @RussKie
    ContributorAuthor

    Awesome! 🎉

  15. Amy-Li03 commented on Aug 30, 2021

    @Amy-Li03
    Contributor

    Verified this issue with .NET 6.0 RC2 latest build: 6.0.10-rc.2.21429.3, when using ApplicationConfiguration.Initialize() method on Program.cs or use top level statements, winforms .NET core project can build and run successfully.

    image
    image

  16. AraHaan commented on Aug 30, 2021

    @AraHaan
    Member

    Does this mean it also adds the [main: STAThread] to the top level program in that case?

  17. RussKie commented on Aug 31, 2021

    @RussKie
    ContributorAuthor

    AFAIK there is no such thing as [main: STAThread], though it is being discussed.
    In the case of top level statements ApplicationConfiguration.Initialize() emits this:

    // Set STAThread
    Thread.CurrentThread.SetApartmentState(ApartmentState.Unknown);
    Thread.CurrentThread.SetApartmentState(ApartmentState.STA);

  18. ghost locked as resolved and limited conversation to collaborators on Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Templatesenhancementwon't fix-compat

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions