8

Based on some requirements, I want to add all constructors/methods of the base class into the derived class without writing methods/constructors in the derived class.

For that I have written code as shown below but it does not work. It shows error "ConsoleApplication1.TestClass2' does not contain a constructor that takes 1 arguments".
How can I fix that?

I cannot create any method or constructor in the base class. Is there any other way apart from inheriting a class?

My code:

namespace ConsoleApplication1
{
    public class TestClass1
    {
        public TestClass1()
        {
            Console.WriteLine("This is base class constructor1");
        }

        public TestClass1(string str1,string str2)
        {
            Console.WriteLine("This is base class constructor2");
        }

        public TestClass1(string str1,string str2,string str3)
        {
            Console.WriteLine("This is base class constructor3");
        }

        public TestClass1(string str1,string str2,string str3,string str4)
        {
            Console.WriteLine("This is base class constructor4");
        }
    }

    public class TestClass2 : TestClass1
    {

    }

    class Program
    {
        static void Main(string[] args)
        {
            TestClass2 test = new TestClass2("test");
            TestClass2 test1 = new TestClass2("test,test");
        }
    }
}
Snostorp
  • 544
  • 1
  • 8
  • 14
Microsoft Developer
  • 5,229
  • 22
  • 93
  • 142
  • 1
    You have no luck. Constructors can't be inherited in C#. Your derived class will have to declare all the constructors you need. It can "chain" the constructors from the base class with `:base(...)` syntax. Your question is a duplicate of [Why can't we use a constructor with parameter in derived classes](http://stackoverflow.com/questions/12167234/) and possibly others. – Jeppe Stig Nielsen Feb 24 '14 at 13:24
  • I'm confused. Is the base class supposed to have these constructors? But you write _I cannot create any method or constructor in base class._ Do you want to add them to the derived class? But you write _without writing methods/constructors in derived class._ Please clarify. – Zev Spitz Jan 23 '18 at 17:08

4 Answers4

17

No, You'll have to explicitly add constructor to your derived classes. Base class's constructor is for base class only. You'll have to chain the constructor of your interested overload.

public class TestClass2 : TestClass1
{
    public TestClass2 (string str1,string str2)
    : base(str1,str2)//Call base constructor explicitly
    {

    }
}

Above sample shows that am particularly interested in constructor overload which takes two string parameters. In this case you are only allowed to use this overload only since TestClass2 doesn't have any other constructors defined.

Compiler will not provide default constructor for you when you define one; So there is only way you can create instance of TestClass2 is new TestClass2(arg1,arg2);

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
5

Try this:

public class TestClass2 : TestClass1
{
    public TestClass2()
    {
    }

    public TestClass2(string str1, string str2)
        : base(str1, str2)
    {
    }

    public TestClass2(string str1, string str2, string str3)
        : base(str1, str2, str3)
    {
    }

    public TestClass2(string str1, string str2, string str3, string str4)
        : base(str1, str2, str3, str4)
    {
    }
}
Guilherme Oliveira
  • 2,008
  • 3
  • 27
  • 44
Only a Curious Mind
  • 2,807
  • 23
  • 39
1

you invoke with single parameter i.e "test" and "test,test"

namespace ConsoleApplication1
{
    public class TestClass1
    {
        public TestClass1()
        {
            Console.WriteLine("This is base class constructor1");
        }


        public TestClass1(string str1, string str2)
        {
            Console.WriteLine("This is base class constructor2");
        }

        public TestClass1(string str1, string str2, string str3)
        {
            Console.WriteLine("This is base class constructor3");
        }

        public TestClass1(string str1, string str2, string str3, string str4)
        {
            Console.WriteLine("This is base class constructor4");
        }
    }

    public class TestClass2 : TestClass1
    {
        public TestClass2(string str)
        {
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            TestClass2 test = new TestClass2("test");
            TestClass2 test1 = new TestClass2("test,test");

        }
    }
}
Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
0
 public TestClass2(string str):base(str) // will invoke base class constructor
 {
 }
Yury Fedorov
  • 14,508
  • 6
  • 50
  • 66
Karty
  • 11