Real Time Use Of Static Constructor

This code shows Real Time Use Of Static Constructor

Reference namespace


use System.collection.generics;

public class Utility
{
public static IDictionary m_Minute = null;
static Utility()
{
m_Minute=new IDictionary();
m_Minute.Add(1,"00");
m_Minute.Add(2,"15");
m_Minute.Add(3,"30");
m_Minute.Add(3,"45");
}
public static IDictionary GetMinute()
{
return m_Minute;
}
}


if you look at above class it is an Utility class and it is consume by Business entity class instance say object Date that has minute component in it.

If we have 10 screens that requires minutes to be displayed in dropdownlist in such cases rather then calling 10 calls to database or constructing this collection we prefer calling one instance at the very load of the application which in turns consume by all the pages and across all the users. In the process we have one instance available for all users and across pages. If one see in normal constructor ,one can notice it is invoked after instance is created whereas static constructor is invoked always first and when it is referenced.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: