一个介绍性的小问题 [英] An introductary small problem
问题描述
// 命名空间声明
使用系统;
// 程序启动类
class InteractiveWelcome
{
// Main开始执行程序。
public static void Main()
{
// 写入控制台/获取输入
Console.Write( 你叫什么名字?:);
Console.Write( Hello,{0}!,Console.ReadLine() );
Console.WriteLine( 欢迎使用C#Station Tutorial!);
}
}
我只是想学习C#语言我注意到了C ++的Main()函数包含一个附加的类!所以我想知道是否有必要定义一个包装Main()函数的类或声明它没有该类太好了!
另一件事我注意到它的Main()函数由公共静态关键字组成,我知道公共的含义但是使用的是什么?我的意思是说我们为什么声明public,因为main()函数应该已经可用于所有代码{global scope}。我无法从任何电子书中得到任何成功的答案,所以任何人都可以帮助我尝试理解它们!
使用访问修饰符public
for entry-point methodMain
只不过是你的幻想。你拥有它只是因为你已经这么写了。它不一定在那里。
请参阅: http://en.wikipedia.org/wiki/Main_function#C.23 [ ^ ]。
-SA
< blockquote> C#是面向对象的语言而不是功能语言,因此 Main
方法封装在类结构中。
public static Main()
是运行时知道从哪里开始程序的约定。
// Namespace Declaration
using System;
// Program start class
class InteractiveWelcome
{
// Main begins program execution.
public static void Main()
{
// Write to console/get input
Console.Write("What is your name?: ");
Console.Write("Hello, {0}! ", Console.ReadLine());
Console.WriteLine("Welcome to the C# Station Tutorial!");
}
}
I was just trying to learn C# language an i noticed that unlike C++ its Main() function includes an additional class wrapped around it! So i am wondering is it necessary to define an class wrapping Main() func or declaring it without that class will too be fine!
Another thing i noticed that its Main() func consists of public static keyword , i know the meaning of public but whats the use? i mean to say that why are we declaring public as the main() function should be already available to all the code{ global scope}. I couldnt any succesful answer from any E-book, so anybody could help me try to understand them!!
The use of the access modifierpublic
for the entry-point methodMain
is nothing but your fantasy. You have it just because you've written so. It doesn't have to be there.
Please see: http://en.wikipedia.org/wiki/Main_function#C.23[^].
—SA
C# is an object orientated language not a functional one hence theMain
method is encapsulated within a class structure.
public static Main()
is a convention for the runtime to know where to start the program from.
这篇关于一个介绍性的小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!