Friday, 3 May 2013

What is Static Class in C#?

If a class declaration includes the keyword static then that class is termed as static class. Static class is a specialized class that cannot have any objects.

First of all, the difference between a non-static and a static class in C# is simply that a static class cannot be instantiated. This means that you don’t use the new keyword to instantiate an instance of the class. Instead, you will access members of the static class by using the class name. Basically, a static class is intended to be a container for a set of methods that only work with supplied parameters and don’t have a need to store or retrieve data that is unique to them.

there are two main features of a static class, one is no object of static class can be created and another is, 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, we do not need to make any instance of this class ,all members can be accessible with its own name.

Static Members
A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine.

Static class members are declared using the static keyword before the return type of the member, for example:

public class Automobile
    public static int NumberOfWheels = 4;
    public static int SizeOfGasTank
            return 15;
    public static void Drive() { }
    public static event EventType RunOutOfGas;

    //other non-static fields and properties...

Static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called. To access a static class member, use the name of the class instead of a variable name to specify the location of the member. For example:

int i = Automobile.NumberOfWheels;

Characteristics of Static Class in C#
  • Members of Static Class Can Only Be Static
  • Static Class Can Contain Only Static Constructor
  • Static Class is Implicitly Sealed
  • Static Class Cannot Be Declared as Sealed
  • Static Class Cannot be Declared as Abstract
  • Static Class Should Inherit Only Object
  • Static Class Cannot be Inherited

When to Use Static Classes
Suppose you have a class CompanyInfo that contains the following methods to get information about the company name and address.

class CompanyInfo
    public string GetCompanyName() { return "CompanyName"; }
    public string GetCompanyAddress() { return "CompanyAddress"; }


These methods do not need to be attached to a specific instance of the class. Therefore, instead of creating unnecessary instances of this class, you can declare it as a static class, like this:

static class CompanyInfo
    public static string GetCompanyName() { return "CompanyName"; }
    public static string GetCompanyAddress() { return "CompanyAddress"; }

Use a static class as a unit of organization for methods not associated with particular objects. Also, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods. It is useful to organize the methods inside the class in a meaningful way, such as the methods of the Math class in the System namespace.

No comments:

Post a Comment