Does not contain a constructor that takes 0 arguments

后端 未结 5 1547
悲哀的现实
悲哀的现实 2020-11-30 03:33

I get an error stating "Products does not contain a constructor that takes 0 arguments" from the following code:

public class Products
{
    string          


        
5条回答
  •  不知归路
    2020-11-30 04:32

    the constructor of the inherited class needs to construct the base class first. since the base class does not have a default constructor (taking 0 arguments) and you are not using the non-default constructor you have now, this won't work. so either A) add a default constructor to your base class, in which case the code of the descending class needs no change; or B) call the non-default constructor of the base class from the constructor of the descending class, in which case the base class needs no change.

    A

    public class Products
    {
        public Products() { }
    }
    
    public class FoodProducts : Products
    {
        public FoodProducts() { }
    }
    

    B

    public class Products
    {
        public class Products(args) { }
    }
    
    public class FoodProducts : Products
    {
        public FoodProducts(args) : base(args) { }
    }
    

    some of this is explained rather OK on msdn here.

提交回复
热议问题