Tuesday 14 January 2014

What is Static Class ?


  • Static Class is same as of non Static class, the only difference i.e. its can not be instantiated means you can not create new instance of the static class.
  • In other words you cannot use NEW keyword to create a variable of class type.
  • For static class ,static constructor is called only once before the class is referenced for the first time in a program loaded once is life time of program and a static class remains in memory for the lifetime of the application domain in which your program resides. 
  • You can directly access the members of a static class by using the class name itself.
          For Example:
          If you have a class with name ClassA and it has public method name MethodA 
          ,then we can access method of class as shown below.
                         
                                         ClassA.MethodA();
      


  •  A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields  
                For example:
    • In the .NET Framework Class Library, the static System.Math class contains       methods that perform mathematical operations, without any requirement to store or retrieve data that is unique to a particular instance of the Math class. That is, you apply the members of the class by specifying the class name and the method  name, as shown in the following example.
                          double dub = -3.14;
                         Console.WriteLine(Math.Abs(dub));



     The following list provides the main features of a static class:
  • Contains only static members.
  • Cannot be instantiated.
  • Is sealed.
  • Cannot contain Instance Constructors(Instance constructors are used to create and initialize any instance member variables when you use the new expression to create an object of a class).
 

 
                                                                                       **      For more details please go through the Link

          

      0 comments:

      Post a Comment