简体   繁体   English

从另一种方法调用Main

[英]Call Main from another method

Is there a way to call Main() from another method "manually"? 有没有办法从另一种方法“手动”调用Main() 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. 我有简单的控制台计算器,当我在function()计算和打印结果时,我想在Main()方法中再次使用例如“输入两个数字:”。

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 null作为参数
  2. Make the parameter optional 使参数可选

null as parameter null作为参数

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. 你不能删除Main函数中的参数,因为它被其他一些东西调用,你不想修改。

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] : 如果你在main函数中使用args参数,null可能不是一个好主意,那么你应该用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. 如果您将它与null一起使用,则可以在不使用之前检查null值的情况下使用它时获取NullReference异常 This can be done by two ways: 这可以通过两种方式完成:

  1. Using an if condition 使用if条件
  2. Null propagation 空传播

An if condition would look like this: if条件看起来像这样:

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. 顺便说一句,你在Main()调用后忘记了一个分号。


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 . 也许你应该重新考虑你的代码设计,因为你在main方法中调用code方法和code方法中的main方法,这可能导致无限循环,因此在StackOverflow异常中 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: 您可以考虑将code方法中要执行的code放在另一个方法中,然后在main方法内部和code方法内部调用:

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 . 但是,因为这是通常occurrs在学习如何代码开头的问题,你应该经历这样一个教程这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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