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

前端 未结 5 660
醉梦人生
醉梦人生 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:22

    This is covered in section 10.11.4 of the C# language spec

    If a class contains no instance constructor declarations, a default instance constructor is automatically provided. That default constructor simply invokes the parameterless constructor of the direct base class

    Here Hello has no defined constructor hence the compiler inserts the default do nothing constructor which just calls the base / object version

提交回复
热议问题