58

I'm making my constructors a bit more strict by removing some of my empty constructors. I'm pretty new to inheritance, and was perplexed with the error that I got: Base Class Doesn't Contain Parameterless Constructor. How can I make A2 inherit from A without there being an empty constructor in A. Also, for my own personal understanding, why is A2 requiring an empty constructor for A?

Class A{
    //No empty constructor for A
    //Blah blah blah...
}

Class A2 : A{
    //The error appears here
}
sooprise
  • 22,657
  • 67
  • 188
  • 276
  • 2
    you cannot create an object of a class with no constructor. if you do not want the parameterless one create a constructor which accepts some parameters. – Davide Piras Oct 07 '11 at 15:55
  • 1
    I should have been more clear in the OP, both class A and A2 have constructors with parameters, I just wanted to remove the parameterless ones so that to create an instance of a class, you need all of the necessary parameters for the class to work. This seems like the intuitive thing to do, is this a smart way to go about doing things? – sooprise Oct 07 '11 at 18:06

6 Answers6

99

In class A2, you need to make sure that all your constructors call the base class constructor with parameters.

Otherwise, the compiler will assume you want to use the parameterless base class constructor to construct the A object on which your A2 object is based.

Example:

class A
{
    public A(int x, int y)
    {
        // do something
    }
}

class A2 : A
{
    public A2() : base(1, 5)
    {
        // do something
    }

    public A2(int x, int y) : base(x, y)
    {
        // do something
    }

    // This would not compile:
    public A2(int x, int y)
    {
        // the compiler will look for a constructor A(), which doesn't exist
    }
}
Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
  • 3
    I've never seen this before, but this is exactly what did the trick. Thank you so much! – sooprise Oct 07 '11 at 17:15
  • Turns out that you can ignore the parameter-less constructor if you set the other constructors to use base constructor with params. I.E. the second definition in A2 is the only one required. – Tyeth Aug 10 '17 at 12:20
  • 1
    @Tyeth True! But every constructor must explicitly call a base class constructor if there is no parameter-less constructor in the base class. If no base constructor is specified, the compiler will try to find a parameter-less constructor in the base class. – Platinum Azure Aug 11 '17 at 04:32
  • If you trying to do this with an injected logging, such cross-cutting concern issue is better handled with a derived base class decorator or an observer logger. – Alan Jun 21 '18 at 12:54
8

Example:

class A2 : A
{
   A2() : base(0)
   {
   }
}

class A
{
    A(int something)
    {
        ...
    }
}
Joe
  • 122,218
  • 32
  • 205
  • 338
3

If your base class doesn't have a parameterless constructor, you need to call one from your derived class using base keyword:

class A
{
    public A(Foo bar)
    {
    }
}

class A2 : A
{
    public A2()
        : base(new Foo())
    {
    }
}
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
1

It has to call some constructor. The default is a call to base().

You can also use static methods, literals, and any parameters to the current constructor in calls to base().

  public static class MyStaticClass
    {
        public static int DoIntWork(string i)
        {
            //for example only
            return 0;
        }
    }

    public class A
    {
        public A(int i)
        {
        }
    }

    public class B : A
    {
        public B(string x) : base(MyStaticClass.DoIntWork(x))
        {
        }
    }
Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
0

when you create the object of your derived class,your base class constructor gets called automatically.So at the time you create your derived class object,and your derived class object has not constructor taking one or more arguments, there will be nothing to pass to the base class constructor that wants one argument. so to do that, you need to pass something to the base class constructor as follows:

Class A{
    //No empty constructor for A
    //Blah blah blah...
}

Class A2 : A{
    public A2():base(some parameter)
}
Srijan
  • 1
0

Because if A has no default constructor then the constructor of A2 needs to call base() with the arguments to the constructor of A. See this question: Calling the base constructor in C#

Community
  • 1
  • 1
Dennis
  • 2,607
  • 3
  • 21
  • 28