0

According To Microsoft Class always contain Default Constructor which is private then how is possible to initiate the Object in another Class.

  • 4
    Where did you read that? – Yuval Itzchakov Jul 21 '15 at 13:16
  • constructor | private | All¹ – Sanjay Yadav Jul 21 '15 at 13:21
  • https://msdn.microsoft.com/en-us/library/kcfb85a6.aspx – Sanjay Yadav Jul 21 '15 at 13:24
  • 1
    You read that part about "classes that contain static members only"? – Ralf Jul 21 '15 at 13:26
  • Note that if you do not use an access modifier with the constructor it will still be private by default. However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated. – Sanjay Yadav Jul 21 '15 at 13:27
  • Welcome to stack overflow, recommended reading [ask] – Dan Beaulieu Jul 21 '15 at 13:33
  • http://stackoverflow.com/questions/2521459/what-are-the-default-access-modifiers-in-c constructor | private | All¹ this link also shows the same makes confusion for me pls suggest. – Sanjay Yadav Jul 21 '15 at 13:41
  • @SanjayYadav the difference is "the accessibility for a constructor without an access modifier" vs "what is the access modifier for the default constructor". If you specify a constructor as `ClassName()` it will have a accessibility of private. By default classes without constructors have a `default public constructor`. – Kritner Jul 21 '15 at 13:58

3 Answers3

1

You've missed the most important part of the article:

A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class

The common use case they show in the article talks about using static members only via the said class, like this:

class NLog
{
    // Private Constructor: 
    private NLog() { }

    public static double Foo = 3.284;
}

Classes do not have default private constructor, they have a public one (except for abstract classes):

If the class is abstract then the declared accessibility for the default constructor is protected. Otherwise, the declared accessibility for the default constructor is public. Thus, the default constructor is always of the form

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
1

You are simply misreading that page. For a class without a defined constructor:

class C {}

There exists a default, public constructor, C().

If you explicitly declare a constructor, then that default constructor won't exist. If you declare your constructor like:

class C
{
    C()
}

then it will be private, as all class members are private unless an explicit public, internal or protected access modifier is used.

David Arno
  • 42,717
  • 16
  • 86
  • 131
0

"The declaration of the empty constructor prevents the automatic generation of a default constructor" (https://msdn.microsoft.com/en-us/library/kcfb85a6.aspx)

If you declare an empty constructor (whether it is private or not, it doesn't matter), you prevent the generation of the default one (which is always public).

cangosta
  • 1,174
  • 2
  • 13
  • 27