Click here to Skip to main content
13,143,614 members (32,311 online)
Rate this:
Please Sign up or sign in to vote.
See more:
if we have a "constructor" and the "main()" method whichone will called first on the execution of the form/page?
Posted 19-Feb-13 23:48pm
Fred Flams 20-Feb-13 5:54am
Well, there is no correct answer to that question I guess.
I you "Main" method is static and declared as your application entry point then it will be called first, otherwise only the constructor will be called
boogac 20-Feb-13 6:03am
constructor can be static also..there will be answers sure but i think best way debug and see
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Main is a static entry point method for your application. When the application starts, Main is called first. After that it is not called anymore in normal circumstances. Note that Main, does not "construct" anything.! If you look at a normal C# winform application it will say something like this:

static class Program {
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static void Main() {
                  //new Form1 CONSTRUCTS the form.
		  Application.Run(new Form1());

The Constructor is called when you generate an object from a class with the new keyword.
It is the method that "constructs" the "object" from a template = the "class". This is done by instantiating the variables, event, methods, ...

Hope this helps.
CHill60 20-Feb-13 6:04am
My 5. Nice, concise and clear
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

No, it doesn't work like that! :laugh:

There is (or should be) only one Main method per project / application, and it will be called once and only once when your application starts, before any objects are constructed. If will then construct the objects needed to run you application and that includes the form in a WinForms app. Normally, you don't need to even look at it, much less change it! :D

Every time you create an instance of a class, the class constructor will be called immediately - as it is the code which actually builds the object instance and sets it starting values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100