hi
Can any one tell me
Static blocks execute first than Main method....?
and why we static the main method.....?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sandeep ShekhawatRank #2,439 · 577 reputationPosted Jun 8, 2011, 3:24 AM EDT
http://blog.sharptrainer.in/index.php/archives/66
VulpesRank #55 · 29.2K reputationPosted Jun 8, 2011, 3:48 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jun 8, 2011, 3:57 AM EDT
The output of this program is:
Sam HobbsRank #107 · 20.5K reputationPosted Jun 8, 2011, 4:56 PM EDT
The one thing that has not been made clear is that the Main method is called by Windows. Windows is language-agnostic (language-independent); it can execute programs written in a variety of computer langauges and compiled by a variety of compilers. Windows can execute programs written in C#, C++, VB .Net, VB 6, COBOL or many other languages as long as the compiler generates the a compatible executable file.
The C# language could have been designed so that a runtime initialization routine instantiates a specified class but that is not how it was designed. It would also be confusing for beginners such as you if the startup function was not static because the beginner would complain about not being able to create more than one instance of their class. The Main method can be called only once per execution and making it static makes it clear that there can only be one instance of the method executing.
Posted Jun 8, 2011, 3:19 AM EDT
http://www.dotnetspider.com/forum/152203-why-do-we-need-static-keyword-for-Main-method.aspx
http://www.dotnetperls.com/static-method
http://www.blackwasp.co.uk/CSharpStaticBehaviour.aspx
Rohatash KumarRank #12 · 57.7K reputationPosted Jun 8, 2011, 3:31 AM EDT
Static - Static methods are shared methods. They can be called with the class name and static method name only. No need of instance to call static methods.
example
class Program
{
public class test
{
static string name;
static int age;
static test()
{
Console.WriteLine("Using static constructor to initialize static data members");
name = "Rohatash Kumar";
age = 23;
}
public static void display()
{
Console.WriteLine("Using static function");
Console.WriteLine(name);
Console.WriteLine(age);
}
}
static void Main(string[] args)
{
test.display();
Console.ReadLine();
}
}