Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can we have two overloaded private constructors in a class?
Posted

1 solution

Yes you can.

C#
 namespace Private_Classes
{
    class Program
    {
        static void Main(string[] args)
        {
        }

        private class Test
        {
            public Test()
            {
            }

            public Test(string Value)
            {
            }

            //these  throws an compile Error "already defines a member called 'Test' with the same parameter types"
            //public Test(string TurnOff)
            //{
            //}

            //public Test()
            //{
            //}
        }
    }
}


But as this forum thread indicates where you would more than likely use an approach.

Private class in C#[^]
 
Share this answer
 
Comments
AnkitGoel.com 10-Dec-12 7:00am    
Simon, the question is about private constructors. do u remember?
Simon_Whale 10-Dec-12 7:04am    
then how would you make a usable private class with 2 private constructors? I'm curious that is why I am asking. As you can't see more than the first default constructor if you attempt to construct the example I've given.
AnkitGoel.com 10-Dec-12 7:06am    
please check this link:

http://www.dotnetperls.com/private-constructor

and

http://msdn.microsoft.com/en-us/library/kcfb85a6(v=vs.71).aspx

hope it helps.
Simon_Whale 10-Dec-12 7:15am    
So to truly answer the OP question the answer should of been NO. As from both the examples that you have shown only allow for a single private constructor as this is used to initalise variables or perform some logic, and I also tried to achieve what he was after in a sample application and couldn't with multiple PRIVATE constructors.

To allow near to his answer the Constructors should be public and the class declaration private.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900