Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# oops
can we have two overloaded private constructors in a class?
Posted 10-Dec-12 1:06am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Yes you can.
 
 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#[^]
  Permalink  
Comments
AnkitGoel.com at 10-Dec-12 7:00am
   
Simon, the question is about private constructors. do u remember?
Simon_Whale at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 165
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100