Monday, September 14, 2009

Static Class

Static classes are used when a class provides functionality that is not specific to any unique instance. Here are the features of static classes in C# 2.0.

Static classes can not be instantiated.
Static classes are sealed so they can not be inherited.
Only static members are allowed.
Static classes can only have static constructor to initialize static members.
Advantages

Compiler makes sure that no instance of static class is created. In previous version of C#, the constructor has to be marked private to avoid this from happening.

Also compiler makes sure that no instance members are declared within a static class.

Sample:

Public static class MyStaticClass
{
Private static int _staticVariable;
Public static int staticVariable;
{
Get
{
Return _staticVariable;
}
Set
{
_staticVariable = value;
}
}
Public static void Function()
{
}
}

--------------***********************----------------------------------------
A static class is defined as a class that contains only static members (of course besides the instance members inherited from System.Object and possibly a private constructor). Some languages provide built-in support for static classes. In C# 2.0, when a class is declared to be static, it is sealed, abstract, and no instance members can be overridden or declared.
public static class File {
...
}
If your language does not have built-in support for static classes, you can declare such classes manually as in the following C++ example:
public class File abstract sealed {
...
}
Static classes are a compromise between pure object-oriented design and simplicity. They are commonly used to provide shortcuts to other operations (such as System.IO.File), or functionality for which a full object-oriented wrapper is unwarranted (such as System.Environment).
DO use static classes sparingly.
Static classes should be used only as supporting classes for the object-oriented core of the framework.
DO NOT treat static classes as a miscellaneous bucket.
There should be a clear charter for the class.
DO NOT declare or override instance members in static classes.
DO declare static classes as sealed, abstract, and add a private instance constructor, if your programming language does not have built-in support for static classes.

Example of Static Class in C#.net

using System;

static class MathFunction {
// Return the reciprocal of a value.
static public double reciprocal(double num) {
return 1/num;
}

// Return the fractional part of a value.
static public double fracPart(double num) {
return num - (int) num;
}

// Return true if num is even.
static public bool isEven(double num) {
return (num % 2) == 0 ? true : false;
}

// Return true of num is odd.
static public bool isOdd(double num) {
return !isEven(num);
}

}

class MainClass {
public static void Main() {
Console.WriteLine("Reciprocal of 5 is " +
MathFunction.reciprocal(5.0));

Console.WriteLine("Fractional part of 4.234 is " +
MathFunction.fracPart(4.234));

if(MathFunction.isEven(10))
Console.WriteLine("10 is even.");

if(MathFunction.isOdd(5))
Console.WriteLine("5 is odd.");

// The following attempt to create an instance of
// MathFunction will cause an error.
// MathFunction ob = new MathFunction(); // Wrong!
}
}

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates