为什么Main()方法应该是静态的? [英] Why should the Main() method be static?

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

问题描述

我试图在C#中创建 public void Main();它说没有找到静态void Main

Main 静态是什么意思?我知道代码对于 public static void Main()可以正常工作。

I tried to create public void Main() in C#; it says no static void Main found.
What exactly does it mean for Main to be static? I know the code works fine for public static void Main().

但是为什么必须是静态

推荐答案

您需要在程序中有一个入口点。静态意味着您可以调用该函数而无需实例化类的对象/实例。有点鸡和蛋 ...在程序内部之前不能实例化对象。

You need an entry point into your program. Static means that you can call the function without having to instantiate an object/instance of a class. It's a bit "chicken and egg"... you can't instantiate an object before you're inside the program.

可以调用静态方法而无需实例化目的。因此 main()必须是静态的,才能使其成为程序的入口。

A static method can be called without instantiating an object. Therefore main() needs to be static in order to allow it to be the entry to your program.

大卫说,您只需在函数定义中添加关键字 static 即可对其进行更改。值得研究静态(类)方法与实例方法,并且有时知道区别可能是有用的。

As David says, you can just add the keyword static to the function definition to change it. It's worth looking into static (class) methods vs instance methods, and knowing the difference can be useful at times.

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

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