.NET Framework: Collections and Generics

The original release of the .NET Framework included collections as .NET was introduced to the Microsoft programming world. The .NET Framework 2.0 introduced generics to complement the System.Collections namespace and provide a more efficient and well performing option.

Introduction to Collections

A collection is a container to which objects can be added. This container offers a powerful flexibility and adaptability. Collections often hold data items that are traversed in a loop structure. All of the items stored in the collection are stored as objects. This means that whatever type you are putting in the collection is taken down to its root object form as it is put in the collection and put back to its native form as it is pulled out. The act of taking a static instance of a particular class down to its base object type and back again is referred to as boxing and unboxing. The example below demonstrates filling an ArrayList, which is a type of collection, and getting the items back out. Notice how the items have to be cast as they are retrieved to return them to their native type.

System.Collections.ArrayList testList = new System.Collections.ArrayList();

// Add some numbers to the list
for (int i = 0; i < 20; i++)
{
  // Go ahead and play here.  You can add whatever type you want in here
  // without issue as you add.  Will be an issue for you at runtime as you
  // retrieve items.
  testList.Add(i);
}

foreach (var item in testList)
{
  // Go ahead and try something like item + 1 here without the cast
  // and see what happens!
  Console.Write("{0}", (int)item);
}

C# introduced the object initialization syntax, which allows you to initialize a collection with values at the time of declaration. This makes collections easier to work with, or at least easy to write example code with! The initializer syntax looks very similar to normal declaration syntax, but adds curly brackets that surround the data being initialized. The following sample code demonstrates the syntax.

int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

There is a performance challenge with collections since they store everything as objects. There is overhead as items are added or removed from a collection because they are boxed and unboxed to a specific type. An additional drawback is that you lose compile time type checking to enforce consistent use of the same type within your collection. There is a workaround for the type check, which involves creating a wrapper class of your own that inherits from CollectionBase and limits the specific type that can be added or removed from the collection. However, if your C# programming is collection intensive, that increases the amount of code you need to write and maintain.

Generics

Generics were introduced in the version 2.0 of the Microsoft framework as a solution to the problems with collections mentioned above. Generics offer a combination of type safety, performance, and generality in the type. A generic is a type safe class that is declared without a specific type applied to its definition. The type is specified at the time the object is actually instantiated and used. The .NET Framework includes a number of generic objects in the System.Collections.Generic namespace. It is likely you will find that many of these will meet your needs. In the event they don't, you can always build your own generic classes as well. The C# tutorial code below is a rewrite of our prior example, but using generics this time. You'll see that you get the compile time checking as well as the automatic type recognition as you retrieve.

List testGeneric = new List();
for (int i = 0; i < 20; i++)
{
  // Go ahead and play here.  You'll get a compile time error here 
  // if you try to add anything other than an int.
  testGeneric.Add(i);
}

foreach (int item in testGeneric)
{
  Console.Write("{0}", item);
}

Summary

We have looked at the original collection that was introduced with the .NET Framework. We discussed the challenges in using it, and introduced Generics which offer a solution to the performance issues and lack of compile time check with collections. It is likely that most of the collection types you will use as a .NET developer going forward will be of the generic variety given the advantages.



About the Author

Mark Strawmyer

Mark Strawmyer is a Senior Architect of .NET applications for large and mid-size organizations. He specializes in architecture, design and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C# for the fifth year in a row. You can reach Mark at mark.strawmyer@crowehorwath.com.

Related Articles

Comments

  • C#.Net application

    Posted by Mahesh on 02/28/2013 09:24pm

    i have required c#.net project by using barcode

    Reply
  • www.sekiedge.com

    Posted by www.sekiedge.com on 10/12/2012 04:52am

    Thanks for sharing your views. Great blog here.. It’s hard to find quality writing like yours these days. I really appreciate people like you. I would like to thank for the efforts you have put in writing this blog. I am hoping the same high-grade blog post from you in the upcoming days as well. A web designer must be very knowledgeable.

    Reply
  • forensic accountant

    Posted by forensic accountant on 10/06/2012 05:57am

    I noticed your blog title !. Everything is very open and represents very clear explanation of issues. Really blogging is spreading its wings quickly. Your write up is a good example of it. Your website is very useful. Thanks for sharing.

    Reply
  • http://www.voguebloom.com

    Posted by llzzmm on 06/25/2011 08:18pm

    == http://www.voguebloom.com/ ==== http://www.newfashion4biz.com/ is specialized in online service on ugg Jordan air max oakland raiders $30--39; Ed Hardy AF JUICY POLO Bikini $20; Handbags (Coach lv fendi d&g) $30 T shirts (Polo ,edhardy,lacoste) $15 Jean(True Religion,edhardy,coogi) $30 Sunglasses (Oakey,coach,gucci,Armaini) $15 New era cap $15 Bikini (Ed hardy,polo) $20 ACCEPT PYAPAL PAYMENT AND CREDIT CARDS DELIVERY TO YOU DOOR TO DOOR Welcome to: == http://newfashion4biz.com/ ==== Air jordan(1-24)shoes $33 Handbags(Coach l v f e n d i d&g) $35 Tshirts (ed hardy,lacoste) $16 Jean(True Religion,ed hardy,coogi) $30 Sunglasses(Oakey,coach,gucci,A r m a i n i) $16 New era cap $15 Bikini (Ed hardy) $25 FREE sHIPPING,ACCEPT PYAPAL PAYMENT AND CREDIT CARDS DELIVERY TO YOU DOOR TO DOOR. == http://www.newfashion4biz.com/ ====

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • The Red Hat® Standard Operating Environment SOE helps you define, deploy, and maintain Red Hat Enterprise Linux® and third-party applications as an SOE. The SOE is fully aligned with your requirements as an effective and managed process, and fully integrated with your IT environment and processes. Benefits of an SOE: SOE is a specification for a tested, standard selection of computer hardware, software, and their configuration for use on computers within an organization. The modular nature of the Red …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds