Proper Use of Static Functions
May 29th, 2007 by Sameer | Filed under .NET articles.Here is a quick refresher on static functions. A static function is static because you do not need to create an instance of the
class in order to use it. Some popular static functions are located in the Math library, for example, Math.Min(x,y). When you
are calling Math.Min, you do not have to type in Math m = new Math(); before calling the Min function, you simply call it.
Another good example of where you can find static functions is in the SqlHelper Source Code.
Here is some example of code that I feel should have been written as a static function.
if (!this.IsPostBack)
{
DataOperations od = new DataOperations();
myGrid.DataSource = od.GetSalesRecords(_user, _company, _startDate);
}
And The GetSalesRecords function is implemented as follows:
public DataTable GetSalesRecords(int user, int company, DateTime d)
{
SqlParameter[] sqlParams = new SqlParameter[] {
new SqlParameter("@user", user),
new SqlParameter("@company", company),
new SqlParameter("@d", d)
};
string sql = "select * from sales where userid=@user and companyID=@company and recorddate > @d";
return SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, sqlParams);
}
This is a perfectly good example of when this GetSalesRecords(..) function should be implemented as a static function.
Here is a quick checklist to judge whether your function should be static.
- You have no need of class or member variables
- Compact - You have all the information you need passed to you from the parameters
- Stateless - You can do your operation and return the result without having to store anything permanently or keep any state.
Thus, this function should be static! Here’s an idea how you can fix it
1. Make your function obsolete and put the second parameter as false. This will cause it to give a warning on compile, but
will not cause an error and your site will still compile. This allows you to phase it out slowly.
[Obsolete("Use static version instead",false)]
public DataTable GetSalesRecords(int user, int company, DateTime d)
{
return GetSalesRecordCompact(user,company,d);
}
2. Create your overload that is static and will do the actual meat and potatoes of the operation (i.e. in the above example,
all we have to do is copy our original function)
/// <summary>
/// Returns the sales records for the user <paramref name="user"/>
/// in the company <paramref name="company "/> after date <paramref name="d"/>
/// </summary>
/// <param name="user">the user name</param>
/// <param name="company">company id</param>
/// <param name="d">beginning date</param>
/// <returns></returns>
public static DataTable GetSalesRecords(int user, int company, DateTime d)
{
SqlParameter[] sqlParams = new SqlParameter[] {
new SqlParameter("@user", user),
new SqlParameter("@company", company),
new SqlParameter("@d", d)
};
string sql = "select * from sales where userid=@user and companyID=@company and recorddate > @d";
return SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, sqlParams);
As a bonus, you can also put proper XML documentation. As well, create the SqlParameters array in the most efficient way
This way, you do not have any code duplication (all your business logic is in the static function), you have added a note to
future developers to stop using the non-static version, and they will save the original call of creating the object unecessarily!
However, if your function was storing some member variables for the class, and needed to be persisted, then in that case
this whole argument does not apply and you can pretend you never saw this article.
What are some of the consequences of static functions?
- You need to consider thread safety - you have to make sure that the function is ‘thread safe’, i.e. it can be executed
multiple times without each one thread interfering with the other. - More to come here
Cheers to static functions!

Gr8 information on how and when to use static functions.
Thank U very Much !