如何在一个类下创建两个主要方法?可能吗? [英] How can I create under one class two main methods? Is it possible?

查看:65
本文介绍了如何在一个类下创建两个主要方法?可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在一个类下创建两个主要方法?可能吗?



我尝试过:



class Program

{

public static void Main(string [] args)

{

Console.WriteLine(Heloo世界);

Console.ReadLine();

}

public static void Main(字符串名称)

{

Console.WriteLine(Heloo India);

Console.ReadLine();

}

}

How can i create under one class two main methods? is it possible?

What I have tried:

class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Heloo World");
Console.ReadLine();
}
public static void Main(string name)
{
Console.WriteLine("Heloo India");
Console.ReadLine();
}
}

推荐答案

不可能。当两个函数头都相同时,系统如何确定使用哪个入口点?提示:不是。



参数总是作为一个字符串数组传递,即使它们只有一个。



您的代码必须确定传入的参数数量并进行适当调整。你的第二个只接受一个字符串的Main方法永远不会允许你的代码编译,更不用说运行了。
Not possible. When both function headers are identical, how is the system going to determine which entry point to use? Hint: it's not.

Arguments are ALWAYS passed as an array of strings, even if there is only one of them.

Your code has to determine how many arguments were passed in and adjust appropriately. Your second Main method that takes just a single string is never going to allow your code to compile, let alone run.


引用:

我如何在一个类下创建两个主要方法?有可能吗?

How can i create under one class two main methods? is it possible?



不,不可能同时激活2种主要方法。

如果合并,你可以拥有1种以上的主要方法使用条件编译,每次只编译1个。

main是一个特殊名称,只能在代码中存在一次,它是你的应用程序的入口点。它不能被其他方法调用,或者至少它是不合理的。


No, it is not possible to have 2 main methods active at same time.
You can have more than 1 main method if combined with conditional compilation so that only 1 is compiled every time.
main is a special name and can exist only once in code, it is the entry point of your app. It can't be called by other methods or at least it is not reasonable.


这篇关于如何在一个类下创建两个主要方法?可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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