October 9, 2006

The NameObjectCollection Class

Filed under: .NET — Phillip Holmes @ 1:33 am

I ran across this in the System.Collections.Specialized namespace and thought it was a very interesting class.

The only downside of NameValueCollection is that it will only accepts strings as types even though it is based on hashtable. The tremendous benefits to using this class are that you can have multiple values per key AND your values are not reordered.

C#:

  1. using System;
  2. using System.Collections.Specialized;
  3. using System.Text;
  4.  
  5.  
  6. static void Main() {
  7.     NameValueCollection stFoo = new NameValueCollection();
  8.     stFoo.Add("akey", "value1");
  9.     stFoo.Add("akey", "value2");
  10.     stFoo.Add("akey", "value3");
  11.     stFoo.Add("bkey", "value1");
  12.     stFoo.Add("bkey", "value2");
  13.     stFoo.Add("bkey", "value3");
  14.     string[] a_foo = stFoo.GetValues("akey");
  15.     foreach (string it in a_foo) {
  16.         Console.WriteLine(it);
  17.     }
  18.     string[] b_foo = stFoo.GetValues("bkey");
  19.     foreach (string it in b_foo) {
  20.         Console.WriteLine(it);
  21.     }
  22. }

July 31, 2006

The HybridDictionary Class

Filed under: .NET — Phillip Holmes @ 12:00 am

If you've ever studied .NET, you'd know that there are many ways to do the same thing. Of course, this is true in almost all programming languages. The HybridDictionary class is an good example of redundant functionality in .NET. In this case, it is an excellent way to make sure the programmer is using the correct collection type. Hats off to Microsoft on this one.

Implemented in the System.Collections.Specialized namespace, HybridDictionary actually decides which collection type to use depending on the size of your collection. If your collection contains less than ten items, it will automagically load your collection via the ListDictionary class. If your collection contains greater than ten items, it will automagically transfer your data to a Hashtable for better performance.

Here is a great example of its use.

July 4, 2006

The Beauty of Generics

Filed under: .NET — Phillip Holmes @ 12:00 am

Many C# and Java developers have been slow to embrace generics. I think this is simply due to the fact that the name is so generic . The question really is, 'What they are for?'. At the shop that I work for, we employ a three tier architecture. Most of the applications there are still in C# 1.1 and some very smart people within the organization mandated that a collection tier be used that would guarantee 'type safe' collections. Unfortunately, this can lead to a performance hit when value types are boxed to be treated as reference types (objects) and unboxed to a value type. That is where generics come in and why they were so anticipated with the .NET 2.0 release.

Generics can be used with any data type. Also, you can mix your value types within the same collection. In a nutshell, generics make your collections "type safe".

This is a console application demonstration of how generics can be utilized:

C#:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace genericsDemo
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             // using the List collection class
  11.             List<widget> Widgets = new List</widget><widget>();
  12.             Widgets.Add(new Widget("tin-foil hat"));
  13.             Widgets.Add(new Widget("European swallow"));
  14.             Widgets.Add(new Widget("African swallow"));
  15.             Widgets.Add(new Widget(1));
  16.             foreach (Widget it in Widgets)
  17.                 Console.WriteLine(it.WidgetName);
  18.                 // give a pause to review your good work
  19.                 Console.Readline("hit any key to... blah blah blah");
  20.         }
  21.     }
  22.     public class Widget
  23.     {
  24.         private object _widgetName = "";
  25.         public object WidgetName
  26.         {
  27.             get { return _widgetName; }
  28.             set { _widgetName = value; }
  29.         }
  30.         // constructor that takes an object argument
  31.         public Widget(object widgetName)
  32.         {
  33.             this._widgetName = widgetName;
  34.         }
  35.     }
  36. }

This is an interface class demonstration of how generics and be utilized:

C#:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Text;
  5.  
  6. // Author:
  7. // Phillip B. Holmes - 09.08.2006
  8.  
  9. namespace BL
  10. {
  11.  
  12.     public interface iGenericCol<mytype>
  13.     {
  14.         void Add(string col, myType value);
  15.         void Remove(myType value);
  16.         HybridDictionary GetDictionary();
  17.         object search(string key);
  18.         void Clear();
  19.     }
  20.  
  21.     // for key pair based structs
  22.     public class GenericCol<mytype> : iGenericCol<mytype>
  23.     {
  24.  
  25.         private HybridDictionary m_list = new HybridDictionary();
  26.  
  27.         public HybridDictionary GetDictionary()
  28.         {
  29.             return m_list;
  30.         }
  31.  
  32.         public void Add(string Col, myType Value)
  33.         {
  34.             m_list.Add(Col,Value);
  35.         }
  36.  
  37.         // removes a value
  38.         public void Remove(myType Value)
  39.         {
  40.             m_list.Remove(Value);
  41.         }
  42.  
  43.  
  44.         // Searches for a key.
  45.         public object search(string Key)
  46.         {
  47.             if (m_list.Contains(Key))
  48.             {
  49.                 return m_list[Key];
  50.             }
  51.             else
  52.             {
  53.                 return null;
  54.             }
  55.         }
  56.  
  57.         public void Clear()
  58.         {
  59.              m_list.Clear();
  60.         }
  61.  
  62.     }
  63. }

The Holmes Blog