Sunday, July 26, 2009

Static Class and Static Method and Static Member

I am assuming that you know static class and static members refer http://msdn.microsoft.com/en-us/library/79b3xss3.aspx to know about it.

Let’ examine the static class and static members in detail using below points

1. Static Class and NonStatic Class are same?


Lets Create static class like below:

  public static class StaticClass
    {
        public static void MyFirstMethod()
        {
            //Code Goes Here
        }
        public static void MySecondMethod()
        {
            //Code Goes Here
        }
    }


MyFirstMethod and MySecondMethod can be called using class name like below.

StaticClass.MyFirstMethod();        
StaticClass.MySecondMethod();


And non static class like below:

public class NonStaticClass
    {
        public static void MyFirstMethod()
        {
            //Code Goes Here
        }


        public static void MySecondMethod()
        {
            //Code Goes Here
        }
    }



MyFirstMethod and MySecondMethod can be called using class name like below.

NonStaticClass.MyFirstMethod();  
NonStaticClass.MySecondMethod();



StaticClass and NonStaticClass have same behavior so far. If the behavior is same why do we need static class, let’s examine it more.

2. Static Class and NonStatic Class are not same because Static class is sealed?



As we know static class is sealed, we can’t inherit static class, let’ create a class MyClass and try to inherit static class like below:

 class MyClass : StaticClass
    {    

    }



You will get compile time error “'StaticClassTest.MyClass': cannot derive from static class 'StaticClassTest.StaticClass'

Now make the NonStaticClass which we have just created as sealed like below:

   public sealed class NonStaticClass
    {
        public static void MyFirstMethod()
        {
            //Code Goes Here
        }

        public static void MySecondMethod()
        {
            //Code Goes Here
        }
    }



Now inherit NonStaticClass to MyClass


class MyClass : NonStaticClass
    {       


    }



You will get compile time error “'StaticClassTest.MyClass': cannot derive from sealed type 'StaticClassTest.NonStaticClass'”

Still StaticClass and NonStaticClass are same, So far all the features provided by StaticClass can be achieved using NonStaticClass. If the behaviour same what is the difference, let’s examine it more

3. Static Class and NonStatic Class are not same because Static class cannot contain instance constructors?

Let’s modify the StaticClassTest and make it like below which is having constructor

public static class StaticClass
    {
        static StaticClass(string MyName)
        {
        }

        public static void MyFirstMethod()
        {
            //Code Goes Here
        }

        public static void MySecondMethod()
        {
            //Code Goes Here
        }
    }



You will get compile time error “'StaticClassTest.StaticClass.StaticClass(string)': a static constructor must be parameterless”

Let’s modify the NonStaticClass and make it like below which is having constructor


public sealed class NonStaticClass
    {
        public NonStaticClass(string MyName)
        {
            //Code Goes Here
        }


        public static void MyFirstMethod()
        {
            //Code Goes Here
        }


        public static void MySecondMethod()
        {
            //Code Goes Here
        }
    }



Compile the application, it will compile without any error.

Call MyFirstMethod of NonStaticClass like below

NonStaticClass.MyFirstMethod();



Put a break point on the constructor of NonStaticClass. You will notice constructor won’t be called only MyFirstMethod will called.

Now create object of NonStaticClass like below

NonStaticClass objNonStaticClass = new NonStaticClass("test");


Constructor of NonStaticClass will be called at this time.

Conclusion:

Normal class with all static members is equivalent to static members of static Class, if you don't intend to inherit normal class.
Normal sealed class with all the static members is equivalent to static members of static Class.
Normal sealed class with all static members and a paramerized constructor is not equivalent to static members of static Class because static class doesn’t permit paramerized constructor.

Decision to use static class
Static classes should be used when a class doesn't have any behaviour as such. Like utility or helper class, the methods in utility and helper class need to be called several times, there is no need to create instance of the class on each call. This will improve the performance as object creation is not involved on each call.

Why static class will improve performance?

There is division of memory within a process

1. Stack
2. Heap
3. Static, which is High Frequency Heap.

The member data of static class is stored in High Frequency Heap which is handled differently than regular object heap. This area of heap is not garbage collected. It means GC doesn't have overhead like regular class which need to be garbage collected when it is unreachable. Static class doesn't get constructed until it is used.

1 comment:

vailerabey said...

Casino Vegas | Las Vegas, NV - Mapyro
Casino 여수 출장샵 Vegas. Las Vegas Metropolitan 상주 출장샵 Area. Casino. 성남 출장안마 Casino. Casino. Strip. 나주 출장안마 MGM Grand. MGM 울산광역 출장마사지 Grand Casino. MGM Resorts World. MGM Resorts International. Las Vegas.

Site Meter