默认构造函数在C#中的含义是什么?

-1 .net c# oop constructor default-constructor

根据我读过的内容,构造函数可以有两个含义:

在计算机编程语言中,术语默认构造函数可以指在没有任何程序员定义的构造函数的情况下由编译器自动生成的构造函数(例如,在Java中)

和

在其他语言中(例如在C++中),无论构造函数是自动生成还是使用定义,都可以调用构造函数而无需提供任何参数.

所以在C#的上下文中,默认构造函数意味着什么,它是指自动生成的构造函数,它唯一的工作是将成员初始化为某些默认值?

sca*_*tag 6

在C#中,默认构造函数是编译器在您未定义任何构造函数时为您生成的空构造函数(没有参数).


VMA*_*Atm 5

Default constructor in C# is, by definition, a constructor with such signature:

Class()
{

}
Run Code Online (Sandbox Code Playgroud)

No parameters are provided here, so the compiler can call this method without any doubts. If your class is providing some other constructors, the default one woudn't be generated by a compiler and you have to add it manually:

Class() : this(null)
{
}

Class(object data)
{

}
Run Code Online (Sandbox Code Playgroud)

In default constructor you can define your logic for a class which is respresenting it's state, such as a private fields or outer components. The other purpose of the default constructors can be found in Dependency Injection containers, there it used for a default instantiation of the object you are mapping.