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#:
-
using System;
-
using System.Collections.Specialized;
-
using System.Text;
-
-
-
static void Main() {
-
NameValueCollection stFoo =
new NameValueCollection
();
-
stFoo.Add("akey", "value1");
-
stFoo.Add("akey", "value2");
-
stFoo.Add("akey", "value3");
-
stFoo.Add("bkey", "value1");
-
stFoo.Add("bkey", "value2");
-
stFoo.Add("bkey", "value3");
-
string[] a_foo = stFoo.GetValues("akey");
-
foreach (string it in a_foo) {
-
Console.WriteLine(it);
-
}
-
string[] b_foo = stFoo.GetValues("bkey");
-
foreach (string it in b_foo) {
-
Console.WriteLine(it);
-
}
-
}
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.
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#:
-
using System;
-
using System.Collections.Generic;
-
using System.Text;
-
namespace genericsDemo
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
// using the List collection class
-
List<widget> Widgets =
new List</widget><widget>
();
-
Widgets.
Add(new Widget
("tin-foil hat"));
-
Widgets.
Add(new Widget
("European swallow"));
-
Widgets.
Add(new Widget
("African swallow"));
-
Widgets.
Add(new Widget
(1));
-
foreach (Widget it in Widgets)
-
Console.WriteLine(it.WidgetName);
-
// give a pause to review your good work
-
Console.Readline("hit any key to... blah blah blah");
-
}
-
}
-
public class Widget
-
{
-
private object _widgetName = "";
-
public object WidgetName
-
{
-
get { return _widgetName; }
-
set { _widgetName = value; }
-
}
-
// constructor that takes an object argument
-
public Widget(object widgetName)
-
{
-
this._widgetName = widgetName;
-
}
-
}
-
}
This is an interface class demonstration of how generics and be utilized:
C#:
-
using System;
-
using System.Collections.Generic;
-
using System.Collections.Specialized;
-
using System.Text;
-
-
// Author:
-
// Phillip B. Holmes - 09.08.2006
-
-
namespace BL
-
{
-
-
public interface iGenericCol<mytype>
-
{
-
void Add(string col, myType value);
-
void Remove(myType value);
-
HybridDictionary GetDictionary();
-
object search(string key);
-
void Clear();
-
}
-
-
// for key pair based structs
-
public class GenericCol<mytype> : iGenericCol<mytype>
-
{
-
-
private HybridDictionary m_list =
new HybridDictionary
();
-
-
public HybridDictionary GetDictionary()
-
{
-
return m_list;
-
}
-
-
public void Add(string Col, myType Value)
-
{
-
m_list.Add(Col,Value);
-
}
-
-
// removes a value
-
public void Remove(myType Value)
-
{
-
m_list.Remove(Value);
-
}
-
-
-
// Searches for a key.
-
public object search(string Key)
-
{
-
if (m_list.Contains(Key))
-
{
-
return m_list[Key];
-
}
-
else
-
{
-
return null;
-
}
-
}
-
-
public void Clear()
-
{
-
m_list.Clear();
-
}
-
-
}
-
}