为什么main()函数在C#中应该是静态的? [英] Why main() function should be static in c#?

查看:103
本文介绍了为什么main()函数在C#中应该是静态的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们必须将main函数声明为static?


静态void main()
{


}

解决方案

您需要在程序中有一个入口点.并且static 确保您可以在不创建入口点类实例的情况下执行此操作.第一个例子?一开始就没有任何实例是非常简单自然的约定.您知道Occam Razor以及为何如此重要: http://en.wikipedia.org/wiki/Occam''s_razor [ ^ ]?

—SA


唯一的原因是因为这是语言的设计者决定将其作为程序入口点的原因.他们可能还使用了完全不同的方法来找到入口点,例如使用元数据或为您实例化对象.将其命名为void main()的主要原因是,它对于来自其他语言的用户来说是直观的,然后将其静态化是合理的,如Abhinavs和SAKs帖子所述.


Why we have to declare main function as static?


static void main()
{


}

解决方案

You need an entry point into your program. And static makes sure you can do this without creating an instance of the entry point class.


If it is not static, it is the instance one, so: what should create the very first instance? Not having any instance in the very beginning is very simple and natural convention. Do you know Occam Razor and why it is important: http://en.wikipedia.org/wiki/Occam''s_razor[^]?

—SA


The only reason is because that''s what the designers of the language decided to be what to look for as an entry point for your program. They could just as well have used a totally different approach to find the entry point, e.g. using meta data, or instantiating an object for you. The main reason for naming it void main() is that it''s intuitive for users coming from other languages, and then making it static makes sense, as described in Abhinavs and SAKs posts.


这篇关于为什么main()函数在C#中应该是静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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