简体   繁体   中英

Call Main from another method

Is there a way to call Main() from another method "manually"? I have the following code:

static void Main(string[] args) {
    # some code
    function();
}

static void function() {
    #some code
    Main(); # Start again
}

I have for example simple console calculator, when I calculate and print the result in function() , I want to start again with eg "Enter two numbers :" in Main() method.

You have to add the parameter as well. If you don't use the parameter in your main functin you have to possibilities:

  1. Give null as parameter
  2. Make the parameter optional

null as parameter

This would work like that:

static void code()
{
    Main(null);
}

Optional property

Then you'd have to modify the parameter like that:

static void Main (string[] args = null)
//...

You can't delete the parameter in the Main function because it is called by some other stuff, you don't want to modify.

If you do use the args parameter in the main function, null might not be a good idea, then you should replace it by something like new string[0] :

static void code()
{
    Main(new string[0]);
}

However, this isn't valid as optional parameter because optional parameters have to be compile-time constant .

If you use it with null you could get a NullReference exception if you use it without checking the value for null before. This can be done by two ways:

  1. Using an if condition
  2. Null propagation

An if condition would look like this:

static void Main (string[] args = null)
{
    Console.Write("Do something with the arguments. The first item is: ");
    if(args != null)
    {
        Console.WriteLine(args.FirstOrDefault());
    }
    else
    {
        Console.WriteLine("unknown");
    }

    code();
}

Null propagation like this:

static void Main(string[] args = null)
{
    Console.WriteLine("Do something with the arguments. The first item is: " + (args?.FirstOrDefault() ?? "unknown"));

    code();
}

By the way, you forgot a semicolon after your Main() call.


Maybe you should rethink your code design anyway, as you call the code method inside the main method and the main method inside the code method, which may result in an endless loop and therefore in a StackOverflow exception . You could consider to put the code you want to execute from the code method in another method which you'd then call inside the main method and inside the code method:

static void Initialize()
{
    //Do the stuff you want to have in both main and code
}

static void Main (string[] args)
{
    Initialize();
    code();
}

static void code()
{
    if (condition /*you said there'd be some if statement*/)
        Initialize();
}

Here you can get more information about methods. But as this is a problem which normally occurrs at the beginning of learning how to code, you should probably go through a tutorial like this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM