C# 如何从另一个类调用Main?

C# 如何从另一个类调用Main?,c#,console,console-application,calculator,main,C#,Console,Console Application,Calculator,Main,所以我正在制作一个C#控制台程序,它是一个简单的计算器,我只是在学习C# 这里是我想调用main的地方: if (info.Key == ConsoleKey.Escape) { Environment.Exit(0); } else { } 我想为加法、减法、乘法和除法类调用main,这样它就可以回到开头,要求‘按‘A’进行加法’等等 我尝试将“Main();”放在else中,但它给出了一个错误,即“没有给出与Program.Main(String[])的必需形式参数“args”对应

所以我正在制作一个C#控制台程序,它是一个简单的计算器,我只是在学习C#

这里是我想调用main的地方:

if (info.Key == ConsoleKey.Escape)
{
    Environment.Exit(0);
}
else
{
}
我想为加法、减法、乘法和除法类调用main,这样它就可以回到开头,要求‘按‘A’进行加法’等等

我尝试将“Main();”放在else中,但它给出了一个错误,即“没有给出与Program.Main(String[])的必需形式参数“args”对应的参数”


我怎样才能在这个类中调用main,使它转到main的开头呢?

您不会自己调用
main
它被用作应用程序的入口点。通常,您会调用其他方法,例如:

static void Main(string[] args)
{
   while (true) 
   {
        Console.Write("> ");
        string command = Console.ReadLine().ToLower();

        if (command == "add")
        {
            Add(); // Call our Add method
        }
        else if (command == "subtract")
        {
            Subtract(); // Call our Subtract method
        }
        else if (command == "multiply")
        {
            Multiple(); // Call our Multiply method
        }
        else if (command == "exit")
        {
            break; // Break the loop
        }
   }
}

static void Add()
{
    // to-be-implemented
}

static void Subtract()
{
    // to-be-implemented
}

static void Multiply()
{
    // to-be-implemented
}
Main(null); // No array
Main(new string[0]); // An empty array
Main(new string[] {}); // Another empty array
Main(new string[] { "Something" }); // An array with a single entry
这里需要注意的另一件事是它是
Main(string[]args)
args
参数包含一个通过命令行传递给控制台应用程序的参数数组

如果要自己调用
Main
,则需要向其传递一个值,例如:

static void Main(string[] args)
{
   while (true) 
   {
        Console.Write("> ");
        string command = Console.ReadLine().ToLower();

        if (command == "add")
        {
            Add(); // Call our Add method
        }
        else if (command == "subtract")
        {
            Subtract(); // Call our Subtract method
        }
        else if (command == "multiply")
        {
            Multiple(); // Call our Multiply method
        }
        else if (command == "exit")
        {
            break; // Break the loop
        }
   }
}

static void Add()
{
    // to-be-implemented
}

static void Subtract()
{
    // to-be-implemented
}

static void Multiply()
{
    // to-be-implemented
}
Main(null); // No array
Main(new string[0]); // An empty array
Main(new string[] {}); // Another empty array
Main(new string[] { "Something" }); // An array with a single entry

你为什么要打电话给梅因?让main调用另一个函数来启动应用程序。然后你可以调用该函数而不是Main。虽然这是一个可能的重复,但我认为最好是有一个沿着
GetCommand
的方法,而不是将逻辑放在
Main
@DarrenYoung中。这只是一个简单的例子。