C# Constructor - why default constructor is generated by compiler only if there is no constructor, instead of when there is no default constructor

前端 未结 3 877
粉色の甜心
粉色の甜心 2020-12-21 00:57

According to MSDN\'s design guide for constructors,

\"If you don’t explicitly declare any constructors on a type, many languages (suc

3条回答
  •  清歌不尽
    2020-12-21 01:04

    If I define a custom constructor which means my object need initialising in a specific way e.g.:

    class Customer
    {
        public Customer(string name) { this.Name = name; }
        public string Name { get; }
    }
    

    If the compiler also added public Customer() then you could bypass the requirement to initialise a customer with a name.

提交回复
热议问题