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.
I wrote this one to help a guy out that had posted on www.houseoffusion.com. I think it is an interesting approach to solve the problem of converting a binary octet to decimal, so I decided to post it here for posterity's sake.
SQL:
-
DECLARE
-
@myOctet varchar(32),
-
@i int,
-
@power int,
-
@result int
-
SET @myOctet = reverse('00111100')
-
SET @power = len(@myOctet) - 1
-
SET @i = @power + 1
-
SET @result = 0
-
WHILE @i>= 1
-
BEGIN
-
SET @result = @result + substring(@myOctet,@i,1) * Power(2,@power)
-
SET @i = @i - 1
-
SET @power = @power - 1
-
END
-
SELECT @result
Here is a really great vid with some extremely talented musicians.
Click Here to View
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();
-
}
-
-
}
-
}