C#程序中可能有多个main()方法吗? [英] Is it possible to have more than one main() method in a C# program?

查看:327
本文介绍了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:

这篇关于C#程序中可能有多个main()方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆