为什么主必须是静态的,以能够接受的命令行参数? [英] Why does the Main have to be static to be able to accept command line arguments?

查看:113
本文介绍了为什么主必须是静态的,以能够接受的命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须有2个不同的主要方法,1不带任何参数,并与1 字串[] args ,但私人和静态的?

Do I have to have 2 different Main methods, 1 without any arguments and 1 with string[] args but private and static?

推荐答案

没有 - 你只需要有一个方法。事实上,在一类,如果你有一个以上的静态的方法,我不相信你可以指定类作为切入点。

No - you only have to have one Main method. Indeed, if you have more than one static Main method in a class, I don't believe you can specify that class as an entry point.

它的确实的已到,虽然是静态的;该类不能通用的,也不能对方法。它可以是:

It does have to be static though; the class can't be generic and neither can the method. It can be:

  • 在任何辅助功能(如可类)
  • 在虚空或有 INT 的返回值
  • 在无参数或者有一个参数类型的String []
  • Any accessibility (as can the class)
  • Void or have a return value of int
  • Parameterless or have a parameter type of string[]

它是静态的和非通用否则的CLR就必须创建类的实例或工作了,你想要什么类型的参数,分别。它不具有足够的信息来作出任何这些决定 - 但它不需要任何信息为在非通用类的静态非通用方法

It has to be static and non-generic as otherwise the CLR would have to create an instance of the class or work out what type parameter you wanted, respectively. It doesn't have enough information to make either of those decisions - but it doesn't need any information for a static non-generic method in a non-generic class.

这篇关于为什么主必须是静态的,以能够接受的命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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