Consider the following code:
class Program
{
static void Main(string[] args)
{
new Program().Run(args);
}
void Run(string[] args)
{
...
}
...
}
Now this works as I expect it to, and certainly I can’t see anything obviously wrong, but instinctually this code worries me. So I have three questions:
- Is this behavior well defined by the compiler?
- Is this code doing what I expect it to?
- Is this code in keeping with best practices?
Yes, this is valid code and does what you expect it to do.
Main is just a static method. The fact that it is the entry point, just means that the CLR calls it at startup. It is perfectly valid to instantiate a class in a static method, and call methods on it.