Tuesday, 12 April 2016

Features of Static Class


C# provides the important feature to create static classes, there are two main features of a static class:-
  1. No object of static class can be created
  2. A static class must contain only static members. 
Then it is important that what is the main benefit to create a static class, the main benefit of making static class is we do not need to make any instance of this class, all members can be accessible with its own name.

Declaration:

A static class is created by using keyword 'Static' as shown here:

Static class Clasname
{
   //C#
}

One more thing that must be noted in static class, all members must be explicitly specified as static, static class does not automatically make its members static. Static class can contain a collection of static methods.

Example:

using System;
static class Shape
{
    public static double GetArea(double height, double width)
    {
        return height * width;
    }
}
class Ractangle
{
    private void GetRactangleArea()
    {
        Double area;
        area = Shape.GetArea(10, 5);
    }
}

Here 'Shape' is static class, it contain static function GetArea. Rectangle is other class and with in GetArea function can be access without creating instance of Class Shape.

Although a static class cannot have an instance constructor, it can have a static constructor.

If a class is declared as static then the variables and methods must be declared as static.

A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.

The main features of a static class are:-

  • They only contain static members.
  • They cannot be instantiated.
  • They are sealed.
  • They cannot contain Instance Constructors or simply constructors as we know that they are associated with objects and operates on data when an object is created.
Example

static class Registration
{
  //All static member variables
   static int nCollegeId; //College Id will be same for all the students studying
   static string sCollegeName; //Name will be same
   static string sColegeAddress; //Address of the college will also same

    //Member functions
   public static int GetCollegeId()
   {
     nCollegeId = 100;
     return (nCollegeID);
   }
    //similarly implementation of others also.
} //class end

public class student
{
    int nRollNo;
    string sName;

    public GetRollNo()
    {
       nRollNo += 1;
       return (nRollNo);
    }
    //similarly ....
   public static void Main()
   {
     //Not required.
     //Registration objReg= new Registration();

     //.
     int cid= Registration.GetCollegeId();
     string sname= Registration.GetCollegeName();

   } //Main end
}

No comments:

Post a Comment

Featured post

What is SharePoint?

Microsoft SharePoint is an extensible platform that provides a range of products that can help organizations with solution for a variety...