Writing a Collection

  • Thread starter Thread starter Gary van der Merwe
  • Start date Start date
G

Gary van der Merwe

Hi



I want to write my own collection object. I want it to have the following
features.

1.. It must be strongly typed (to a Class that I have written).
2.. I should be able to add and remove items dynamically.
3.. It should have an indexer. This will have a string parameter. The
indexer should return the first item that's ID property is equal to the
parameter. These collections will generally contain less than 10 items, so I
don't intend to use a HashTable.
I really don't know where to start. Dose any one know where I can find a
sample which dose something like this?



Gary
 
Hi,

See the documentation and examples on the System.Collections.DictionaryBase
class. It uses a HashTable internally, but is very convenient for building
strong-typed dictionaries.

On the other hand, Hashtable can be optimized for different amounts of data
stored so there's no reason to worry about its performance.
 
Hi

Thanks. That was just what I was looking for.

I query your point regarding the performance of the Hashtable. Why would MS
then create the System.Collections.Specialized.HybridDictionary ?

Where can I find out how a HashTable works internaly?

Gary


Dmitriy Lapshin said:
Hi,

See the documentation and examples on the System.Collections.DictionaryBase
class. It uses a HashTable internally, but is very convenient for building
strong-typed dictionaries.

On the other hand, Hashtable can be optimized for different amounts of data
stored so there's no reason to worry about its performance.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Gary van der Merwe said:
Hi



I want to write my own collection object. I want it to have the following
features.

1.. It must be strongly typed (to a Class that I have written).
2.. I should be able to add and remove items dynamically.
3.. It should have an indexer. This will have a string parameter. The
indexer should return the first item that's ID property is equal to the
parameter. These collections will generally contain less than 10 items,
so
I
don't intend to use a HashTable.
I really don't know where to start. Dose any one know where I can find a
sample which dose something like this?



Gary
 
Hi,
I query your point regarding the performance of the Hashtable. Why would MS
then create the System.Collections.Specialized.HybridDictionary ?

According to MSDN, this class automatically accomodates the type of internal
storage to the amount of data stored - and this is transparent to the user,
I assume. But Hashtable itself can also be fine-tuned through specifying the
so called "load factor".

I have actually never tested Hashtable performance for small amounts of
data - so there might really be overhead related to managing lookup trees
etc. As for the internal implementation, I think there should be something
well-known (probably from the Donald Knuth's book on algorithms).

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Gary van der Merwe said:
Hi

Thanks. That was just what I was looking for.

I query your point regarding the performance of the Hashtable. Why would MS
then create the System.Collections.Specialized.HybridDictionary ?

Where can I find out how a HashTable works internaly?

Gary


Dmitriy Lapshin said:
Hi,

See the documentation and examples on the System.Collections.DictionaryBase
class. It uses a HashTable internally, but is very convenient for building
strong-typed dictionaries.

On the other hand, Hashtable can be optimized for different amounts of data
stored so there's no reason to worry about its performance.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Gary van der Merwe said:
Hi



I want to write my own collection object. I want it to have the following
features.

1.. It must be strongly typed (to a Class that I have written).
2.. I should be able to add and remove items dynamically.
3.. It should have an indexer. This will have a string parameter. The
indexer should return the first item that's ID property is equal to the
parameter. These collections will generally contain less than 10
items,
 
Gary,
I posted an ExDictionaryBase VB.NET sample class that works mostly like
DictionaryBase except it allows you to use HybridDictionary instead of
HashTable, actually it allows you to use any IDictionary instead of
HashTable. I wrote it for just this reason, where I want a type safe
dictionary but not use HashTable.

See
http://groups.google.com/[email protected]&rnum=1

However currently I do not see my post with the attachments on google. If
you cannot locate the attachments I can send them to you.

I really need to get them on www.gotdotnet.com

Hope this helps
Jay

Gary van der Merwe said:
Hi

Thanks. That was just what I was looking for.

I query your point regarding the performance of the Hashtable. Why would MS
then create the System.Collections.Specialized.HybridDictionary ?

Where can I find out how a HashTable works internaly?

Gary


Dmitriy Lapshin said:
Hi,

See the documentation and examples on the System.Collections.DictionaryBase
class. It uses a HashTable internally, but is very convenient for building
strong-typed dictionaries.

On the other hand, Hashtable can be optimized for different amounts of data
stored so there's no reason to worry about its performance.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Gary van der Merwe said:
Hi



I want to write my own collection object. I want it to have the following
features.

1.. It must be strongly typed (to a Class that I have written).
2.. I should be able to add and remove items dynamically.
3.. It should have an indexer. This will have a string parameter. The
indexer should return the first item that's ID property is equal to the
parameter. These collections will generally contain less than 10
items,
 
You can use a ListDictionary object to save resources. Microsoft
recommends this for collections that contain less than 10 items
because it uses a linked list. A simple sample follows..

public class MyCollection
{
private ListDictionary mList = new ListDictionary();
public object this[string Key]
{
set
{
if(!this.mList.Contains(Key))
this.mList.Add(Key, value);
else
mList[VarName] = value;
}
get
{
if(this.mList.Contains(Key))
return this.mList[Key];
else
return null;
}
}
public void Remove(string Key)
{
if(this.mList.Contains(Key))
this.mList.Remove(Key);
else
return;//You may throw an exception here...

}
}

I just implemented indexer and remove because indexers set method
dynamically adds.
 
Zubeyr Dereli said:
You can use a ListDictionary object to save resources. Microsoft
recommends this for collections that contain less than 10 items
because it uses a linked list. A simple sample follows..

public class MyCollection
{
private ListDictionary mList = new ListDictionary();
public object this[string Key]
{
set
{
if(!this.mList.Contains(Key))
this.mList.Add(Key, value);
else
mList[VarName] = value;
}
get
{
if(this.mList.Contains(Key))
return this.mList[Key];
else
return null;
}
}
public void Remove(string Key)
{
if(this.mList.Contains(Key))
this.mList.Remove(Key);
else
return;//You may throw an exception here...

}
}

I'm assuming that the "VarName" bit in the setter is meant to be "Key".

Note that the above can be written as:

public class MyCollection
{
private ListDictionary mList = new ListDictionary();

public object this[string Key]
{
set
{
mList[Key]=value;
}
get
{
return mList[Key];
}
}

public void Remove(string Key)
{
mList.Remove(Key);
}
}

There's no need for all the checking for whether or not the list
contains the key - the indexer of ListDictionary does everything for
you.
 
Hi All



Thanks for all your answers and examples. It has been a great help.



For the examples, when adding a item to the collection, one has to pass two
objects, a key and a value. Generally the key is a string, and the value is
of some type. The type of the value objects that I want to add to my
collection has a ID property. I want this property to be the key. How would
I do this.



Gary
 
Gary,
Have you tried something like:

public class MyItem
{
//property ID
}
public class MyCollection
{
public void Add(MyItem item)
{ this.mList.Add(item.ID, item);
}
}

You cannot do it with just the indexer as the indexer expects a key and
returns or accepts an object.

Hope this helps
Jay
 
You can find out some of the answers by looking at the source to Rotor.

However, there is a simple explanation.

The HybridDictionary uses a list for less than 10 items. It uses a
Hashtable for more than that.

There is an overhead, both in performance and in memory usage for a
hashtable. If you are using a whole lot of dictionaries, say in a
hierarchy, you may have a few that have lots of items, and many that have
only a few items. By dynamically adjusting the underlying type, you get
better performance and near optimal memory usage in this case.

If you were to use only Hashtable dictionaries, the memory usage would be
much higher, and the performance would be slightly slower.

If you were to use only list-based dictionaries, the memory usage would be
optimal, but the performance on searches on the larger nodes would be
terribly slow.


Again, the Rotor hashtable will shed a lot of light on this if you feel like
digging. Otherwise, just look up hash based searches in any college level
algorithms book.


Gary van der Merwe said:
Hi

Thanks. That was just what I was looking for.

I query your point regarding the performance of the Hashtable. Why would MS
then create the System.Collections.Specialized.HybridDictionary ?

Where can I find out how a HashTable works internaly?

Gary


Dmitriy Lapshin said:
Hi,

See the documentation and examples on the System.Collections.DictionaryBase
class. It uses a HashTable internally, but is very convenient for building
strong-typed dictionaries.

On the other hand, Hashtable can be optimized for different amounts of data
stored so there's no reason to worry about its performance.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Gary van der Merwe said:
Hi



I want to write my own collection object. I want it to have the following
features.

1.. It must be strongly typed (to a Class that I have written).
2.. I should be able to add and remove items dynamically.
3.. It should have an indexer. This will have a string parameter. The
indexer should return the first item that's ID property is equal to the
parameter. These collections will generally contain less than 10
items,
 
Back
Top