How does this actually work? I thought Main
was supposed to be "called". But how is that possible if it's marked private?
public class Program
{
private static void Main()
{
}
}
How does this actually work? I thought Main
was supposed to be "called". But how is that possible if it's marked private?
public class Program
{
private static void Main()
{
}
}
From Jon Skeet on bytes.com:
Basically, the execution of the main method is started by special code within the CLR (or possibly code driving the CLR to start with) which doesn't need to obey the same rules.
Also, there's another question that covers this topic here already.
Following to MSDN the Main method should not be public:
Main is declared inside a class or struct. Main must be static and it should not be public. (In the earlier example, it receives the default access of private.) The enclosing class or struct is not required to be static.
It is a language implementation detail, the CLR simply reads the EntryPointToken value from the assembly header and performs no accessibility checks on the method with that token. The underlying call is _AppDomain.ExecuteAssembly(). So we'll need to turn to the C# Language Specification, section 3.1 mentions the accessibility rule explicitly:
In C#, every method must be defined as a member of a class or struct. Ordinarily, the declared accessibility (§3.5.1) of a method is determined by the access modifiers (§10.3.5) specified in its declaration, and similarly the declared accessibility of a type is determined by the access modifiers specified in its declaration. In order for a given method of a given type to be callable, both the type and the member must be accessible. However, the application entry point is a special case. Specifically, the execution environment can access the application's entry point regardless of its declared accessibility and regardless of the declared accessibility of its enclosing type declarations.
The bolded section documents what the CLR does with the EntryPointToken. The C# compiler could verify accessibility if it wanted to, but doesn't.
the Main method is executed by CLR when ever you execute your code CLR compiler searches for that Main method. Even if you give main in small letters it wont get called.
Acces modifiers in .Net are (really strong) suggestions. You can call any method or access any property/field using reflection. Consider code like this which behaves somewhat like what is actually going on when main gets called.
public class EntryPointAttribute : System.Attribute
{
public string EntryPoint { get; private set; }
public EntryPointAttribute(string entryPoint) { this.EntryPoint = entryPoint; }
}
public static class EntryPointProcessor
{
public static void Process(object theObject)
{
Type t = theObject.GetType();
var ep = t.GetCustomAttributes(typeof(EntryPointAttribute), true).FirstOrDefault();
string entryPointName = ((EntryPointAttribute)ep).EntryPoint;
MethodInfo mi = t.GetMethod(entryPointName, BindingFlags.Static | BindingFlags.NonPublic);
mi.Invoke(null, new object[0] { });
}
}
[EntryPoint("anentrypoint")]
public class entryPointClass
{
private static void anentrypoint()
{
Console.WriteLine("in anentrypoint");
}
}
class Program
{
static void Main(string[] args)
{
EntryPointProcessor.Process(new entryPointClass());
}
}