Difference between static and private constructor.

Question

Difference between static and private constructor.

closed 0
Technology Ketan 6 months 1 Answer 671 views 0

Answer ( 1 )

  1. => The static constructor will only be executed once.
    The private constructor will be executed each time it is called.
    => The static constructor cannot have parameters.
    The private Constructor may have parameters
    => A static constructor is called before the first instance is created. So it’s kind of global initialize.
    Private constructor is called after the instance of the class is created. Inheritance-wise both are same.
    => A class can have only one static constructor
    A class can have multiple private constructors
    => Ex.
    —————————————————–
    Static Constructor
    —————————————————–
    public static class Class1
    {
    static Class1()
    {
    }
    }
    —————————————————–
    Private Constructor
    —————————————————–
    public class Class2
    {
    private Class2()
    {
    }
    }

Leave an answer