Click here to Skip to main content
15,889,211 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if we have a "constructor" and the "main()" method whichone will called first on the execution of the form/page?
Posted
Comments
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

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:

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

                  //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.
 
Share this answer
 
Comments
CHill60 20-Feb-13 6:04am    
My 5. Nice, concise and clear
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900