结构中的 Main() 与 class 之间有区别吗?

Is there a difference between Main() in a structure versus a class?

假设在 C# 中,我的 Main() 函数位于 Entry class 中,它的存在仅是为了容纳入口点。我会这样做:

public class Entry
{

    public static void Main()
    {

        ...

    }

}

我认为这很典型,至少在工作中的一些 Java 项目中,我看到 classes 仅针对 main() 函数而存在,并且从未想过它。但是,虽然我一直在学习更多关于 C# 和结构的知识,但我尝试执行以下操作:

public struct Entry
{

    public static void Main()
    {

        ...

    }

}

它在视觉上的效果完全一样。因此,假设您在 C# 中的入口点仅包含您的 Main() 函数,那么在运行时将其容器设为 structclass 相比有任何实际差异吗?

答案是,关于入口点(和您的约束),除了这里和那里的几个字节外,没有明显的区别。但是,让我们访问文档

Main() and command-line arguments (C# Programming Guide)

The Main method is the entry point of a C# application. (Libraries and services do not require a Main method as an entry point.) When the application is started, the Main method is the first method that is invoked.

Overview

  • The Main method is the entry point of an executable program; it is where the program control starts and ends.
  • Main is declared inside a class or struct. Main must be static and it need not be public. (In the earlier example, it receives the default access of private.) The enclosing class or struct is not required to be static.

...