In C#, a private constructor is a constructor method declared with the private
access modifier (or without mentioning any modifier). If a class have only a private constructor and does not have any public constructor, then you are not able to create the instance of a class. Example 👇
public class Student
{
Student() { } // private constructor
}
public class Program
{
static void Main()
{
Student stu = new(); //Error CS0122 'Student.Student()'
// is inaccessible due to its protection level
}
}
When you try to create the instance of stu, you will the following error.
[Read More]