C#程序中可能有多个main()方法吗?

C#程序中可能有多个main()方法吗?

问题描述:

在C#控制台应用程序中是否可以有多个 main()方法(具有相同的参数)?如果是,怎么办?

Is it possible to have more than one main() method (with the same parameters) in a C# console application? If so, how?

您可以有多个主要方法,可以指定将哪个方法用作该方法的入口点。您的编译器提供的应用程序。请参阅链接以获取更多详细信息

You can have more than one main method, you can specify which to use as the entry point for the application by your compiler.. See this link for more detail

示例:

using System;
using System.Collections.Generic;
using System.Text;


    namespace Multiple_MainClasses
    {
        class A
        {
            static void Main(string[] args)
            {
                Console.WriteLine("I am from Class A");
                Console.ReadLine();
            }
        }
        class B
        {
            static void Main(string[] args)
            {

                Console.WriteLine("I am from Class B");
                Console.ReadLine();
            }
        }
    }

何时运行此代码,您将得到编译错误。要解决此问题,请在解决方案资源管理器中转到项目属性,或者按ctrl + alt + L,转到应用程序选项卡,然后选择要执行的方法,如下所示:

When you will run this code, you will get compilation error. To resolve go to project properties in solution explorer or press ctrl + alt + L, go to application tab and Select Class with method which you want to execute as shown below: