First of all, yes, you can call Main
. Please note that in your example you are missing the args
parameters (which would have the command line arguments). Besides that, also know that Main
returns, and then execution continues after where you called it. Also, you would very likely end up with a stack overflow. If I recall correctly modern versions of the JIT can optimize tail recursion, yet I do not believe they do for this particular case.
About that newbie-level explanation of a loop... this is an infinite loop, it will run forever:
while(true)
{
// ...
}
You can exit the loop with break
while(true)
{
// ...
if (something)
{
break;
}
// ...
}
Or you can change that true
for a conditional:
while(!something)
{
// ...
}
The idea would be to wrap Main
in a loop, so that it would continue to run until a condition is met. You could have another method and call it in the loop, for example:
public static bool Execute()
{
if (something)
{
// do whatever
return true;
}
return false;
}
public static void Main(string[] args)
{
while (!Execute())
{
// Empty
}
}
There are, of course, other kinds of loops aside from the while loop, they are the do...while loop, for loop, and foreach loop.
Goto would work? Arguably. The idea is that you can use goto to control the flow of execution to back in the method. That is:
label:
// ...
goto label;
The above code is an infinite loop. You can, of course, introduce a condition:
label:
// ...
if (!something)
{
goto label;
}
// ...
The while can do – sometimes with the help of a variable – whatever you can do with goto, and it is usually easier to read and less error prone.
Restarting the process is a bit more tricky, you need to use Process.Start to run your own executable. See What is the best way to get the executing exe's path in .NET?.