Why is the .ctor() created when I compile C# code into IL?

前端 未结 5 699
醉梦人生
醉梦人生 2021-01-04 02:15

With this simple C# code, I run csc hello.cs; ildasm /out=hello.txt hello.exe.

class Hello
{
    public static void Main()
    {
        System.         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-04 02:37

    It's the default parameterless constructor. You're correct; it doesn't do anything (besides passing on to the base Object() constructor, which itself doesn't do anything special either anyway).

    The compiler always creates a default constructor for a non-static class if there isn't any other constructor defined. Any member variables are then initialized to defaults. This is so you can do

    new Hello();
    

    without running into errors.

提交回复
热议问题