none
Children inherit parent class appear an error RRS feed

  • Question

  • class Animal { public string name; public Animal(string name) { this.name = name; } } class Dog: Animal //there is an error { } static void Main(string[] args) { Dog dog = new Dog(); dog.name = "小狗"; Console.WriteLine(dog.name); Console.Read(); }

    Anyone can help to point to why there is an error.


    Monday, March 9, 2020 5:44 AM

Answers

  • Hi wonglihuang,

    Thank you for posting here.

    In C#, a derived class is considered an extension of the parent class, it will implicitly gains all the members of the base class, except for its constructors and finalizers.

    In the derived class, the base class's parameterless constructor is implicitly called like this:

        class Animal
        {
            public Animal() { }
        }
    
        class Dog : Animal    
        {
            public Dog():base()
            {
            }
        }


    You can take a look at the explanation in this link.

    How Base classes are automatically instantiated before derived classes

    However, as we know, when there is no explicit constructor in the class, a default parameterless construction is generated, but if we manually add a parameterized construction, this default parameterless construction will no longer be provided.

    Therefore, in your program, the Dog class cannot find the no-argument construct in Animal, and it is necessary to manually add a parameterized construct to call the constructor of the parent class

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by wonglihuang Monday, March 9, 2020 7:31 AM
    Monday, March 9, 2020 7:26 AM

All replies

  • In your class Dog, you also need to declare the same constructor with parameter.

            class Dog: Animal
            {
                // pass directly to base implementation if you have nothing to add
                public Dog(string name): base(name) {}
            }

    Monday, March 9, 2020 5:54 AM
    Answerer
  • Hi wonglihuang,

    Thank you for posting here.

    In C#, a derived class is considered an extension of the parent class, it will implicitly gains all the members of the base class, except for its constructors and finalizers.

    In the derived class, the base class's parameterless constructor is implicitly called like this:

        class Animal
        {
            public Animal() { }
        }
    
        class Dog : Animal    
        {
            public Dog():base()
            {
            }
        }


    You can take a look at the explanation in this link.

    How Base classes are automatically instantiated before derived classes

    However, as we know, when there is no explicit constructor in the class, a default parameterless construction is generated, but if we manually add a parameterized construction, this default parameterless construction will no longer be provided.

    Therefore, in your program, the Dog class cannot find the no-argument construct in Animal, and it is necessary to manually add a parameterized construct to call the constructor of the parent class

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by wonglihuang Monday, March 9, 2020 7:31 AM
    Monday, March 9, 2020 7:26 AM