-2

Why is this code being generated when I create a "c# Windows Forms Application"?

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

The reason I ask is because I've never seen this before. I am used to creating projects and them being empty, just like this.

static class Program
{
    static void Main()
    {

    }
}

Ive never seen that "STAThread" or "Application.etc..." stuff before.

I just upgraded to Visual Studio 2019.

Adam James
  • 152
  • 1
  • 8
  • 1
    ...because that is a starter template that also includes a bare bones definition for `Form1` and you could compile and run and you would see a blank form show up? – crashmstr May 03 '19 at 14:48
  • Possible duplicate of [Why are WinForms applications STAThread by default?](https://stackoverflow.com/questions/4659220/why-are-winforms-applications-stathread-by-default) – Raymond Chen May 03 '19 at 14:49
  • 4
    You sure you haven't created console applications? Because that code has always been there for winforms applications. I think the comments were added at some point but the rest has been there for as long as I can remember. – Lasse V. Karlsen May 03 '19 at 14:49
  • @RaymondChen I think this is about all the stuff that is there and not just `STAThread`, so I don't think that is the best choice for this being a duplicate of. – crashmstr May 03 '19 at 14:50
  • The template would have been quite useless if it only generated a blank entry point without the code that actually starts the application and displays the main form. If you want a truly blank app, then don't use templates. – mm8 May 03 '19 at 14:51
  • 2
    It is not generated, both snippets come from the project templates. Respectively for a Winforms and a Console app. There isn't much they could do to precook console app code, the Winforms code is sufficiently obtuse that new programmers would have a hard time getting that right from scratch. It is all important and all very well documented. Picking up an introductory book about Winforms programming is a good idea since guessing at it rarely turns out well. – Hans Passant May 03 '19 at 14:57
  • Thanks for the responses. It was a bit of blunder on my behalf. I just got confused between the Form1.cs file and Program.cs file – Adam James May 03 '19 at 15:17

2 Answers2

1

probably all the solution/project you do with your old Visual studio version were Console/CommandLine projects. What you create with Visual Studio is a WindowsForm project (when you create the project it open by default a template with an empty form) that is different because it use a "graphic" environment instead of the black and white console you are familiar with.

To be specific what the code do is:

    Application.EnableVisualStyles();

Enables visual styles for the application. Visual styles are the colors, fonts, and other visual elements that form an operating system theme. (MSDN LINK).

    Application.SetCompatibleTextRenderingDefault(false);

Is a property that handle some compatibility with old frameworks. If your application is not updated from a .NET Framework 1.0 or .NET Framework 1.1 application leave it to the default value 'false'. If you not know what i'm saying.... leave it to the default value 'false' :D

    Application.Run(new Form1());

When your application start it launch the form called 'Form1' (you can see it in your solution explorer).

for the STAThread explanation i remand you to the link in comments from @Raymond Chen, but i think is a bit too hard for you, just think that is a "must" for windowsForm application for now :)

Legion
  • 760
  • 6
  • 23
  • Thank you for further explaining what that code does. I was used to seeing the more empty "Form1.cs" file which explains why I was confused to see the extra code that is inside of the "Program.cs" file. – Adam James May 03 '19 at 16:29
0

That is auto-code what visual studio generate to run in Program.cs in any winform project And this is code Program.cs:

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Threading.Tasks;
   using System.Windows.Forms;

   namespace WindowsFormsApplication1
   {
   static class Program
   {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
    }
    }