Instance Constructor Vs Static Constructor in c sharp

Instance Constructor Vs Static Constructor


  • If a constructor is explicitly declared by using a static modifier we call it as a static constructor or else by default every constructor is instance only.
  • As we are aware that constructor are responsible for initializing the variables of a class, Static contractor will take the responsibility of initialing static and constant variables and instance constructor is responsible for initializing instance and read only variables.
  • Static constructor execute immediately once the execution of class start and more over it is the first block of code which execute under the class whereas  instance constructor execute only after creating the instance of the class and each and every time the instance of the class is created.
  • Static contractor is called one and only one time in a life cycle of a class whereas instance constructors are called for zero times if no instance is created and n-time if n instance are created.
  • As we are aware that constructor can be parameterized but it is possible in case of instance constructor but not static constructor because static constructor is implicitly called and the first block of code under a class to execute so there is no chance of sending values to static constructor.









No comments:

Post a Comment