I have read at least one class should contain Main method since the entry point of execution is the Main(). Lets see the code below.
using System;
namespace consoleApplication
{
class sample
{
public static void Main()
{
Console.WriteLine("Demo program");
Console.ReadKey();
}
}
}
We know that a static method inside a class can be invoked using dot operator with the class name. So can we call Main() as
sample.Main();
just like we call
Console.WriteLine();
and kindly tell me whether it is syntactically right or not even if we are not using the same.
By default the class and
Main()method are created as private members. If you absolutely wanted to, you could make them public members then call them from another project.However, I do not know why this would be necessary. This is what libraries are created for. I would instead create a class library and make my static class/methods within it.
Your
Main()method is simply the entry point for your console application. Every application has an entry point. I cannot think of a use case in which you would create several application projects, then call theMain()methods from other projects. I would instead, make a new project that will be used as a library, not an application. This is what a class library is meant for.The answer to the last question, is this syntactically correct? Yes. Again, I stress that this is not good practice. From MSDN “Main must be static and it should not be public.” Just because it can be done, does not mean that it should be.