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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Jun 8, 2011, 8:56 PM
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.
VulpesPosted Jun 8, 2011, 7:57 AM
The output of this program is:
VulpesPosted Jun 8, 2011, 7:48 AM
Sandeep ShekhawatPosted Jun 8, 2011, 7:24 AM
http://blog.sharptrainer.in/index.php/archives/66
Rohatash KumarPosted Jun 8, 2011, 7:31 AM
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();
}
}
Posted Jun 8, 2011, 7:19 AM
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