是否有必要在C#中至少有一个类

use*_*460 3 c# program-entry-point class

在Java中,我们需要至少有一个类来保存main方法.

当我创建一个C#项目时,框架代码看起来类似于Java框架代码.

但是,C#中的main函数是否应该至少有一个类?

namespace Hello_World
{
    class Hello   //Is it compulsory to have this class ?
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.ReadKey();

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:我假设在最基本的C#程序中至少需要一个类,因为主要方法需要包含在类中.由于运行程序需要main(),因此至少需要一个类.我说得对吗?

Sri*_*vel 12

不,不一定.你也可以struct.以下是一个完全有效的c#程序.

namespace NoClass
{
    struct Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello world");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Method应该在一个类型中的事实c#需要一个封闭类型为您的main方法.它可以是structclass.这是你的选择.但你不能在c#中拥有全局功能.