Click here to Skip to main content
13,148,214 members (60,223 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

7.7K views
1 bookmarked
Posted 31 Oct 2011

Using Generic Methods to Avoid Boxing-unboxing

, 31 Oct 2011
Rate this:
Please Sign up or sign in to vote.
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:

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:

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:

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:

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:

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:

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)

Share

About the Author

beatles1692
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170924.2 | Last Updated 31 Oct 2011
Article Copyright 2011 by beatles1692
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid