Click here to Skip to main content
15,884,388 members
Articles / All Topics

Using Generic Methods to Avoid Boxing-unboxing

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
31 Oct 2011CPOL1 min read 12.5K   1  
How to use generic methods to avoid boxing-unboxing

Every time we convert a value type (int, decimal, double, etc.) to a reference type (to an object to be specific), a boxing occurs (As MSDN defines it, it means to put a value type inside heap) and if we convert an object to an int, an unboxing occurs. Here's an article on MSDN that describes it all. I'm working on this method that gets a DataRow, a FieldName and a default value and returns row[fieldname] value (if value is null or DBNull, it returns the default value). My first version of this method is something like this:

C#
public static string GetFieldValue
(DataRow row,
string fieldname,
string defaultvalue)
{
var value=row[fieldname];
if(value==null || value==DBNull.Value)

return defaultvalue;
return value;
}

Later, I want to write an overload for this method to work with int values I write something like this:

C#
public static int GetFieldValue
(DataRow row,
string fieldname,
int defaultvalue)
{
var value=row[fieldname];
if(value==null || value==DBNull.Value)

return defaultvalue;
return (int)value;
}

OK an unboxing is happening here and I can't do anything about it. I think to myself here we have a sign of DRY violation. I can have a single method as a base, something like this:

C#
public static object GetFieldValue
(DataRow row,
string fieldname,
object fieldvalue)
{
var value=row[fieldname];
if(value==null || value==DBNull.Value)
return defaultvalue;
return value;
}

Then, I can rewrite the int version like this:

C#
public static int GetFieldValue
(DataRow row,
string fieldname,
int defaultvalue)
{
return (int)GetFieldValue(row,fieldname,(object) defaultvalue);
}

But wait a second, I can see a boxing here which is not necessary but how can I get rid of it? One solution that comes to my mind is to change my base method to a generic method:

C#
public static T GetFieldValue<T>
(DataRow row,
string fieldname,
T defaultValue)
{
var value=row[fieldname];
if(value==null || value==DBNull.Value)
return defaultvalue;
return (T)value;
}

Again, if T is a value type, an unboxing will occur but there's no boxing and I can rewrite the int version as:

C#
public static int GetFieldValue
(DataRow row,
string fieldname,
int defaultvalue)
{
return GetFieldValue<int>(row,fieldname, defaultvalue);
}

I wrote a test that ran each method for 1,000,000 times and it turns out that the omitted boxing impact is very small (418 ms for object version vs. 358 ms for generic version).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --