Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, June 10, 2017

Static Constructor in C#

A constructor declared using static modifier is a static constructor. A static constructor is use to initialize static data or to perform a particular action that need to be performed only once in life cycle of class.  Static constructor executes one and only one time in life cycle of class. 
Static is a keyword.We can put it before class,function and constructor.We can use static in multiple ways.It is not part of object oriented.
If static is declared with constructor ,it is static constructor. It is used for  performance,memory optimization,code optimization.
It retains value per call but it is not feature of object oriented.
static class YourClass
     static YourClass() 

Features of Static Constructor
1) Static constructor is independent of object of class.constructor is dependent to class but Static constructor  is obsolete independent to object architecture.
2)In static constructor, we can not initialize member variable.
   only static variable can be initialized.
3)If constructor is declared as static,it is static by nature means it is called only cannot change value per call.

Example - We want to develop a lcd type application where we can change volume,brightness.
a)When our application starts first time, we want to detect drivers,sound card  means we want default configuration of hardware --- we can use static constructor as it will call only once.

b) When our application starts first time, we want default configuration like default volume level,brightness level  --- we can use normal constructor .  If user click on reset button, normal constructor will call.

Points to remember

  • Static constructor can not have access modifier
  • We can not pass parameter to static constructor
  • The execution of a static constructor is triggered by the first of the following events to occur within an application domain: 
  • 1)An instance of the class is created.  
  • 2)Any of the static members of the class are referenced.
  • Only one static constructor is called during execution of application
  • static constructor is called first then default constructor is called
  • It is used to initialize static variable that needs to be executed only once.
  • we can not overload static constructor

Example - 
Step 1  - We have a class HomeLoan. In this class,We have static variable RateOfInterest. To initialize it, we have created static constructor static HomeLoan().
Step 2  - We have member variable Amount. To initialize it, we have created constructor public HomeLoan().
Step 3 - When we create object of HomeLoan, we can see that static constructor is called first, then default constructor.
Step 4  - When we create second time object of HomeLoan, we can see that static constructor is not called as it is called only once , but default constructor is called second time.

using System;

using System.Drawing;

namespace StaticConstructorDemo
    public class HomeLoan
        public static int RateOfInterest;
        public int Amount;
        static HomeLoan()
            Console.WriteLine("Static constructor");
            RateOfInterest = 5;

        public HomeLoan(int amount)
            Console.WriteLine("Default constructor");
            this.Amount = amount;

    class Program
        static void Main(string[] args)
            HomeLoan h = new HomeLoan(2000);
            Console.WriteLine("Amount :{0} ,RateofInterest :{1}", h.Amount, HomeLoan.RateOfInterest);

            HomeLoan h1 = new HomeLoan(4000);
            Console.WriteLine("Amount :{0} ,RateofInterest :{1}", h1.Amount, HomeLoan.RateOfInterest);



No comments :

Post a Comment