简体   繁体   中英

Creating a window with Windows.Forms in C#

I am creating windows using Method1

using System;
using System.Windows.Forms;

    static class Program {
        public static void Main(){
        Application.Run(new Form1());
        }
    }

    class Form1:Form 
    {

    public Form1(){
    
        }
    }

Method 2

using System;
using System.Windows.Forms;

    static class Program {
        public static void Main(){
        Form frm=new Form();
        Application.Run(frm);
        }
    }

Which is appropriate while writing c# code. Differentiate both please.

For your case, certainly the first one (though it doesn't matter much), but without the inheritance.

Inheritance should be used if you wanted to extend the Form object to contain more properties and or methods.

static class Program {
        public static void Main(){
        Application.Run(new Form());
        }
    }

But what happens here:

Form frm=new Form();
Application.Run(frm);

You are creating a reference to your Form object. This is not needed, as you are doing nothing with it.

What could you do with it?

Check the methods and properties section in the documentation: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form?view=netcore-3.1#methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM