Inherit an abstract class without any constructor

前端 未结 2 1747
悲&欢浪女
悲&欢浪女 2020-12-07 04:12

I want to inherit a class from another class, marked as abstract, that not have any constructor defined.

This is my code:

// In one assembly (TheMess         


        
2条回答
  •  离开以前
    2020-12-07 04:53

    You probably have an internal constructor (not shown in the code that you posted) and are trying to instantiate the class with the internal constructor from a different assembly.

    (The default constructor for a base class is automatically called from a derived class if you don't explicitly specify a base class constructor to call, so it might not be obvious to you that the base class constructor is being called.)

    For example, if one assembly contains this class (inside namespace ClassLibrary1):

    public class Base
    {
        internal Base()
        {
        }
    }
    

    And a DIFFERENT assembly does this:

    class Derived: Base
    {
        public Derived()
        {
        }
    }
    

    You will see the following compile error:

    The type 'ClassLibrary1.Base' has no constructors defined

提交回复
热议问题