Static Class and Private Constructor in C#

In this Post I am going to explain the difference between static class and declaring private Constructor.
This issue came while I was coding for one of the task.

Static Class in C#:
In C#, a static class is a class that cannot be instantiated. The main purpose of using static classes in C# is to provide blueprints of its inherited classes. Static classes are created using the static keyword in C# and .NET. A static class can contain static members only. You can‘t create an object for the static class.
Also You cannot inherit a static class from other class.

Private Constructor in C#:
It is the implementation of a singleton class pattern.
Using private constructor, prevents the creation of the instances of that class.We cannot use the new keyword and create the object of that class.

Differences between both of them:

public class Test{
   public string FileName
        {
            get;
            private set;
        }
     public string MemberName
        {
            get;
            private set;
        }
  
  private Test(){}
  
  public Test Info(string fileName="",string memberName="" )
  {
    return new Test
    {
       FileName = fileName,
       MemberName = memberName
    }
  }
}


As you can see in the above code, I have declared a private Constructor which prevents using new keyword to create the object of this class but in the method Info() we are allowed to create the object of this class and return it.
In Private Constructor, the object creation is allowed in the current scope but not outside whereas if the class is declared as static we are not allowed to create the object anywhere.

Also This class has been made singleton using private Constructor and only object will be used across the program.

Thanks 🙂