Click here to Skip to main content
Licence CPOL
First Posted 17 May 2009
Views 16,418
Downloads 10
Bookmarked 8 times

A non-generic IList implementation without any Boxing and Unboxing

By gUrM33T | 18 May 2009 | Technical Blog
A simple workaround to avoid boxing and unboxing while still using a non-generic IList implementation
3 votes, 75.0%
1

2

3

4
1 vote, 25.0%
5
2.00/5 - 4 votes
μ 2.00, σa 3.50 [?]

UPDATE (May 18, 2009): The title of this article has created some confusion for fellow CodeProject members, the explanation of which is that my intention was NOT to avoid .NET generics completely but, to present an implementation of "non-generic System.Collections.IList interface" without boxing and unboxing. 

While writing this post, I'm assuming that you know what boxing/unboxing means in the .NET context and in case you don't, read this MSDN article to know more about it.

In the default implementation of IList interface such as ArrayList, when you Add() a ValueType item to the list, the value is boxed inside a Object. Similarly, when a ValueType element is retrieved from the list, unboxing occurs and explicit casting must be performed.

Now, according to MSDN documentation, it can take upto 20 times longer than a simple reference assignment and the casting takes upto four times long as an assignment.

Provided below is my own implementation that gets rid of boxing/unboxing problem while still using the same old fashioned non-generic IList:

class VariantList : VariantListBase
{
  #region Methods
  public int Append<T>(T value) where T : struct {
    return (this as IList).Add(new T[1] { value });
  }

  public T GetAt<T>(int index) where T : struct {
    T[] value = (T[])(this[index]);
    return value[0];
  }
  #endregion
}

VariantListBase is an abstract class implementing IList with a structure similar to the following:

abstract class VariantListBase : System.Collections.IList
{
  private System.Collections.IList innerList = new System.Collections.ArrayList();

  /*********
   * IList, ICollection and IEnumerable method implementations
   * simply forward the call to this.innerList
   *********/
}

So, how does VariantList avoid boxing/unboxing? It achieves this by using arrays.

Remember that any type of array derives from System.Array base class, which itself, is a reference type. And this is exactly what is being done in the Append<T>(T value) method. When Append<T>() is used to add a ValueType element to the list, a new array of one single element of the same type T is created and added, thus completely avoiding the need for boxing. In the same way, when a value is retrieved using GetAt<T>(int index) method, it is retrieved without performing any unboxing process.

But here is the deal: After performing the 10 million iteration benchmarking and using Hi-Res Timer, I haven't found much difference (only a few milliseconds) between the time taken to insert as well as retrieve items using ArrayList and VariantList. And I'll be honest with you, while repeating benchmarks, a few times VariantList actually performed a little slower than the ArrayList.

Anyways, if you want to use this code, perform some benchmarks and see if it's of any advantage to you. Also, if you have any comments, suggestions, criticisms, etc. on this article, please feel free to share.

License

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

About the Author

gUrM33T

Software Developer

Canada Canada

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberzecanard10:41 23 Nov '09  
GeneralI gave it 5. Pinmemberf_numb3r8:51 20 May '09  
GeneralRe: I gave it 5. PinmembergUrM33T13:35 20 May '09  
GeneralMy vote of 1 PinmemberWesner Moise12:47 19 May '09  
GeneralRe: My vote of 1 PinmembergUrM33T13:26 19 May '09  
GeneralProblems PinmemberWesner Moise12:37 19 May '09  
QuestionReally better? Pinsupporterpeterchen4:45 18 May '09  
GeneralMy vote of 1 Pinmemberhaindl23:05 17 May '09  
GeneralInteresting, but... PinmemberJoe Enos13:51 17 May '09  
GeneralRe: Interesting, but... PinmembergUrM33T13:32 18 May '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120209.1 | Last Updated 18 May 2009
Article Copyright 2009 by gUrM33T
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid