Calling the constructor base class in a derived class

Based on some requirements, I want to add all the constructors / methods of the base class to the derived class without writing methods / constructors in the derived class. To do this, I wrote the code as shown below, but it does not work. It shows the error "ConsoleApplication1.TestClass2" does not contain a constructor that takes 1 argument. "How can I get it? I cannot create any method or constructor in the base class. Is there any other way besides class inheritance? Belos is 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");

    }
}
}
+3
source share
4 answers

. . . .

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

    }
}

, , . , TestClass2 .

; , TestClass2 new TestClass2(arg1,arg2);

+11

:

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)
    {
    }
}
+3

i.e "test" "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");

        }
    }
}
0
 public TestClass2(string str):base(str) // will invoke base class constructor
 {
 }
-1

All Articles