Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# VB.NET
if we have a "constructor" and the "main()" method whichone will called first on the execution of the form/page?
Posted 20-Feb-13 0:48am
Fred Flams at 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 at 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 at 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 | :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! Big Grin | :-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
0 OriginalGriff 250
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 DamithSL 125
4 Afzaal Ahmad Zeeshan 120
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190

Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2014
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