why main method in c# is always placed inside the class but not in c++

前端 未结 6 704
野的像风
野的像风 2021-01-01 23:49

Why we put main() method always inside the class in C# while in c++ it always placed outside of the class.

相关标签:
6条回答
  • 2021-01-02 00:35

    C# is complete object oriented language where everything is considered as objects. Hence, Main() is kept inside class.

    0 讨论(0)
  • 2021-01-02 00:38

    Because in .NET you can place methods only inside types. You cannot have them floating around in the empty space. C++ has its legacy from C which is not an OOP language so you could define functions anywhere.

    0 讨论(0)
  • 2021-01-02 00:39

    You cannot place method outside class/struct in C#. Each method must be in class/struct

    0 讨论(0)
  • 2021-01-02 00:45

    For historical reasons. C++ evolved from C, which had a global main() function. C# is much younger and was designed from scratch. One of the design features of C# is the absence of global functions, so the main function has to belong to a class.

    0 讨论(0)
  • 2021-01-02 00:45

    It is a convention. Which is in line with Java (also follows the semantic of having a method inside class).

    0 讨论(0)
  • 2021-01-02 00:54

    The C++ language designers followed the lead of C and so the main function is a plain function.

    The C# language designers made the choice, when designing the language, that all methods must be part of classes.

    0 讨论(0)
提交回复
热议问题