如何从另一个班级调用Main?

-3 c# console program-entry-point calculator console-application

因此,我正在编写一个简单的计算器C#控制台程序,我只是在学习C#。

这是我要称为main的地方:

if (info.Key == ConsoleKey.Escape)
{
    Environment.Exit(0);
}
else
{
}
Run Code Online (Sandbox Code Playgroud)

我想称呼main为加法,减法,乘法和除法类,因此它可以回到开始时询问“按'A'进行加法”等操作的地方。

我尝试放入“ Main();” 在其他情况下,但它给了我一个错误,并说“没有给定的参数对应于'Program.Main(String [])的所需形式参数'args'”

我该如何在此类中调用main,以便转到main的开头?

Llo*_*oyd 5

您不会称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
}
Run Code Online (Sandbox Code Playgroud)

这里要注意的另一件事是Main(string[] args),该args参数包含在命令行上传递给控制台应用程序的参数数组。

如果你打电话给Main自己,你将需要一个值传递到这一点,例如:

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
Run Code Online (Sandbox Code Playgroud)