0

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.

1 Answer 1

2

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://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form?view=netcore-3.1#methods

Sign up to request clarification or add additional context in comments.

2 Comments

Both lines of code create same window form. In Second method no need to define class Form1.
There is no need for Form1 in either case. Especially since you don't extend it really with anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.