Private Constructor In C#

Private Constructor

 The declaration of the empty constructor prevents the automatic generation of a default constructor. Note that if you do not use an access modifier with the constructor it will still be private by default. The private modifier is usually used explicitly to make it clear that the class cannot be instantiated.

Private constructors are used to prevent the creation of instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class

Private Constructor example

Private Constructor In C# 
 
Private Constructor In C#

Notice that if you uncomment the following statement from the example, it will generate an error because the constructor is inaccessible because of its protection level.

 Private Constructor In C#

Constructors can be marked as public, private, protected, internal, protected internal or private protected.
 

Private Protected

 
The private protected keyword combination is a member access modifier. A private protected member is accessible by types derived from the containing class, but only within its containing assembly

The private protected access modifier is valid in C# version 7.2 and later.

Example

A private protected member of a base class is accessible from derived types in its containing assembly only if the static type of the variable is the derived class type. For example, consider the following code segment:

  1. // Assembly1.cs  
  2. // Compile with: /target:library  
  3. publicclassBase {  
  4.     privateprotectedint myRate = 0;  
  5. }  
  6. publicclassDerivedClass1: Base {  
  7.     void Access() {  
  8.         Base baseObject = newBase();  
  9.         // Error CS1540, because myRate can only be accessed by  
  10.         // classes derived from Base.  
  11.         // baseObject.myRate = 5;  
  12.         // OK, accessed through the current derived class instance  
  13.         myRate = 5;  
  14.     }  
  15. }  
  16. // Assembly2.cs  
  17. // Compile with: /reference:Assembly1.dll  
  18. classDerivedClass2: Base {  
  19.     void Access() {  
  20.         // Error CS0122, because myRate can only be  
  21.         // accessed by types in Assembly1  
  22.         // myRate = 10;  
  23.     }  
  24. }  

This example contains two files, Assembly1.cs and Assembly2.cs. The first file contains a public base class, Base, and a type derived from it, DerivedClass1. Base owns a private protected member, myRate, which DerivedClass1 tries to access in two ways. The first attempt to access myValue through an instance of Base will produce an error. However, the attempt to use it as an inherited member in DerivedClass1 will succeed. In the second file, an attempt to access myRate as an inherited member of DerivedClass2 will produce an error, as it is only accessible by derived types in Assembly1.

Struct members cannot be private protected because the struct cannot be inherited.