Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it best to use static functions in ASP.NET?

I have been wondering, when to use static functions, and when not to in ASP.NET?

What are the advantages and disadvantages in using them, in various aspects like performance, following good practices etc (and many more, whichever you feel is relevant).

like image 494
Mahesh Velaga Avatar asked Nov 02 '09 20:11

Mahesh Velaga


1 Answers

Cons:

  • threading issues (static functions don't require an instance to be called on, so it is easy to invoke them from different parts of the code and if they read/write to a shared state this state might be corrupted in a multi-threaded environment such as ASP.NET)
  • difficult to unit test (as static functions don't require an object instance, constructor injection is impossible meaning that the only way to inject dependencies is by passing them as arguments to the function itself)

Pros:

  • performance (this is questionable - in most cases performance gains will be completely negligible compared to other parts of the code)
like image 96
Darin Dimitrov Avatar answered Sep 20 '22 05:09

Darin Dimitrov