Wednesday, 5 August 2015

Static Class. Why and when to use ?

Working in a team,  I come across developers who  are good in coding but get confused when we ask the when and why do we use a static class?

So, if you are one of them,  here's the reason why and when we create a static class: 

We use OOPS concept, so everything is object oriented.Now, objects can be of 2 types 

  1. Real life objects : Like laptop, car, employee etc. For these real life objects we create a regular class.
  2. Virtual Objects : These are the objects which encapsulate some action on real life objects.Like break, appreciate, bless, curse. These are those which are not restricted. You can break a rock or a car or an employee. All these are different objects but the break action can be used for all.So, here we use Static objects and functions.
Beside there are other reasons as well:
  1. Performance consideration :  CLR does not specify when it is created but static class remains in memory for the lifetime of the application domain in which your program resides. So, static methods are faster invoked.
  2. Its sealed, cannot be instantiated.So, reduces typing effort etc.

Happy coding :)Please feel free to suggest more on this.

No comments:

Post a Comment