Partial Class Constructor

1.8k views Asked by At

I have a partial class with a no parameter constructor in a first file. For this partial class, I would like to have another constructor with a parameter in second file.

Unfortunately I get this error :

Class doesn't contain parameterless constructor

I can't add it in the second file, because it already exists in first file.

What's wrong ? Thanks.

First file :

namespace NS
{
    public partial class A
    {
        public A() {...}
    }
}

Second file :

namespace NS
{
    public partial class A
    {
        public A(int MyParam) {...}
    }
}
1

There are 1 answers

0
Duncan Carr On
namespace NS
{
    class Program
    {
        static void Main()
        {
            var _a1 = new A();
            var _a2 = new A(123);

            System.Console.ReadKey();
        }
    }

    public partial class A
    {
        public A()
        {
            System.Console.WriteLine("Hi from A (parameterless)");
        }
    }

    public partial class A
    {
        public A(int MyParam)
        {
            System.Console.WriteLine("Hi from A (int)");
        }
    }
}

I hope this is what you are after. Sorry if I misunderstood. I am new to this.