Static Keyword and Main Method Functionalities in C#

Introduction

In order to memory management some keywords are playing vital role in development programming languages. In this article you will learn depth information about static keyword.

error

You cannot find definition for static, abstract, sealed and more keywords. Those definitions strictly ruled into compilers. So a rule has been framed into compiler to enclosed main with static keyword. Here i want to say difference between c# compiler and other programming compiler (java).

  1. class Program  
  2. {  
  3.     public static void Display()  
  4.     {  
  5.         Console.WriteLine("Static Method");  
  6.     }  
  7. }  
  8. Static void main()  
  9. {  
  10.     Program objprogram = new Program();  
  11.     objprogram.Display();  
  12. }  
'Program.Display()' cannot be accessed with an instance reference; qualify it with a type name instead.

It comes when you trying to call static methods from object reference. But it error won't come in java if program object reference trying to call static methods.

Why C# framed such rule for static?

C# made some special features on compiler to predict some errors and issues. In other programming we will find lot of errors after compilation and won’t produce proper results at run time, Compiler gives error free screen but won't produce output. To makes better interpreter works for C# framed rules to produce errors and violations. So these rules helped us to follow better principles to programming.

Interpreter finds main function and start execution from it. Before compiler check rules,
If main enclosed by static keyword or not.

Why Main function should enclosed Static Keyword?

If main not enclosed with static keyword, you have a chance to initialize from somewhere in your program.

For more updates for keep watching on TechnetWiki.

Next Recommended Reading Main Method In C#