473,785 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Collection vs Dictionary

Hello,

There's some confusion about the purpose and difference between these handy
classes...

First, both of them are holding number of key - value pairs, right? Then, I
see that there may be some difference in terms of data types allowed for
keys and values, perhaps?

I read the following in MSDN about "CollectionBase " class and
"DictionaryBase " classes:
CollectionBase: Provides the abstract (MustInherit in Visual Basic) base
class for a strongly typed collection.
DictionaryBase: Provides the abstract (MustInherit in Visual Basic) base
class for a strongly typed collection of key-and-value pairs

Hey, this implicitly signals that Collection is *not* collection of
key-and-value pairs, which is certainly not true. Then what's the truth?

The System.Hashtabl e class seems to be the most flexible representative of
Dictionary classes, having both key and value types "object". Would that
imply that if either key of value type is anchored to one specific, it is no
more to be called "Hastable"?
-- Pavils
Nov 16 '05 #1
11 18709
Pavils Jurjans <pa****@mailbox .riga.lv> wrote:
There's some confusion about the purpose and difference between these handy
classes...

First, both of them are holding number of key - value pairs, right?
Wrong. Collections only need to be collections of values - no need for
keys.
Then, I
see that there may be some difference in terms of data types allowed for
keys and values, perhaps?

I read the following in MSDN about "CollectionBase " class and
"DictionaryBase " classes:
CollectionBase: Provides the abstract (MustInherit in Visual Basic) base
class for a strongly typed collection.
DictionaryBase: Provides the abstract (MustInherit in Visual Basic) base
class for a strongly typed collection of key-and-value pairs

Hey, this implicitly signals that Collection is *not* collection of
key-and-value pairs, which is certainly not true. Then what's the truth?
No, it is true - ICollection is *not* a collection of key/value pairs.
It's a collection of values. Where did you get the idea that it's a
collection of key/value pairs?
The System.Hashtabl e class seems to be the most flexible representative of
Dictionary classes, having both key and value types "object". Would that
imply that if either key of value type is anchored to one specific, it is no
more to be called "Hastable"?


I didn't quite follow that sentence, but you can certainly use value
types in hashtables, as either keys or values. They just end up getting
boxed.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hello John,
No, it is true - ICollection is *not* a collection of key/value pairs.
It's a collection of values. Where did you get the idea that it's a
collection of key/value pairs?
My understanding of term "collection " comes from VB, especially programming
with classic ASP prior to transerring all ASP developement to JScript couple
of years ago. So, in classic ASP, for example, I get "A collection of form
variables" in Request.Form object. Since they are readable by providing key,
I was under impression that collection certainly is key-value pair holding
structure. So, as far as I see from your comments, C# collection should not
be confused with VB collection. Soo.. is there a "place" of item within
collection, or it's like a sack with stuff where order of thnks is
irrelevant? I see references to "first item in collection" in many places,
so obviously there is some sort of order. But then, what point is to have
collection instead of array?
The System.Hashtabl e class seems to be the most flexible representative of Dictionary classes, having both key and value types "object". Would that
imply that if either key of value type is anchored to one specific, it is no more to be called "Hastable"?


I didn't quite follow that sentence, but you can certainly use value
types in hashtables, as either keys or values. They just end up getting
boxed.


I mean, most of the time I don't need "object" key type, it's sort of
overkill. In fact, most of the time I just need string type key and object
type value. A good candidate for this seems to be StringDictionar y, and I
hope that using it could have slight effect on code efficiency.
Regards,

Pavils
Nov 16 '05 #3
Pavils,
In addition to Jon's comments.
My understanding of term "collection " comes from VB, especially programming with classic ASP prior to transerring all ASP developement to JScript couple
A VB6/ASP Collection is not a .NET collection.

Within .NET you can use Microsoft.Visua lBasic.Collecti on to have the same
functionality as a VB6 Collection.

Rather then using Microsoft.Visua lBasic.Collecti on I use either ArrayList &
a HashTable, or create type safe versions by inheriting from CollectionBase
or DictionaryBase.

Hope this helps
Jay

"Pavils Jurjans" <pa****@mailbox .riga.lv> wrote in message
news:uf******** ******@TK2MSFTN GP10.phx.gbl... Hello John,
No, it is true - ICollection is *not* a collection of key/value pairs.
It's a collection of values. Where did you get the idea that it's a
collection of key/value pairs?
My understanding of term "collection " comes from VB, especially

programming with classic ASP prior to transerring all ASP developement to JScript couple of years ago. So, in classic ASP, for example, I get "A collection of form
variables" in Request.Form object. Since they are readable by providing key, I was under impression that collection certainly is key-value pair holding
structure. So, as far as I see from your comments, C# collection should not be confused with VB collection. Soo.. is there a "place" of item within
collection, or it's like a sack with stuff where order of thnks is
irrelevant? I see references to "first item in collection" in many places,
so obviously there is some sort of order. But then, what point is to have
collection instead of array?
The System.Hashtabl e class seems to be the most flexible representative
of
Dictionary classes, having both key and value types "object". Would
that imply that if either key of value type is anchored to one specific, it

is no more to be called "Hastable"?


I didn't quite follow that sentence, but you can certainly use value
types in hashtables, as either keys or values. They just end up getting
boxed.


I mean, most of the time I don't need "object" key type, it's sort of
overkill. In fact, most of the time I just need string type key and object
type value. A good candidate for this seems to be StringDictionar y, and I
hope that using it could have slight effect on code efficiency.
Regards,

Pavils

Nov 16 '05 #4

"Pavils Jurjans" wrote...
Soo.. is there a "place" of item within
collection, or it's like a sack with stuff
where order of thnks is irrelevant?
That is depending on which implementation of ICollection you're using.
ICollection is just an interface, that doesn't care about the order of
items.
I see references to "first item in collection" in many
places, so obviously there is some sort of order. But
then, what point is to have
collection instead of array?
Well, an array *is* actually an implementation of ICollection...

The advantage to use some other class than ordinary arrays for collections
is that the size can be altered "dynamicall y" by just adding new items.
The System.Hashtabl e class seems to be the
most flexible representative of Dictionary classes,
having both key and value types "object". Would that
imply that if either key of value type is anchored
to one specific, it is no
more to be called "Hastable"?


As *all* types in .NET really are of type object through inheritance, you
can use instances of anything for the key and value respectively.
I mean, most of the time I don't need "object" key
type, it's sort of overkill. In fact, most of the
time I just need string type key and object
type value.
A string is an object.
A good candidate for this seems to be
StringDictionar y, and I hope that using it
could have slight effect on code efficiency.


A StringDictionar y "implements a hashtable with the key strongly typed to be
a string rather than an object." [From the documentation].

// Bjorn A
Nov 16 '05 #5
Hello Bjorn

The advantage to use some other class than ordinary arrays for collections
is that the size can be altered "dynamicall y" by just adding new items.
I was thinking of ArrayList there

A StringDictionar y "implements a hashtable with the key strongly typed to be a string rather than an object." [From the documentation].


Yes, I actually tested today and found out that StringDictionar y actually
enforces *both* key and value to be of string type. That makes me wonder why
then there is the NameValueCollec tion, which is also a collection of string
key-value pairs. It escapes me why should I use speciffically the one or the
other.

-- Pavils
Nov 16 '05 #6

"Pavils Jurjans" wrote...
The advantage to use some other class than ordinary
arrays for collections is that the size can be altered
"dynamicall y" by just adding new items.
I was thinking of ArrayList there


Well, both ArrayList as well as ordinary arrays implements the interface
ICollection.

*Which* implementation to choose is in many cases a choice of preference.
That makes me wonder why then there is the
NameValueCollec tion, which is also a collection
of string key-value pairs. It escapes me why should
I use speciffically the one or the other.


A NameValueCollec tion works in a slightly different manner, as it's foremost
a "sorted collection", which means that you can refer to a single item both
through the key as well as using an index.

// Bjorn A


Nov 16 '05 #7
Bjorn,
a "sorted collection", which means that you can refer to a single item both through the key as well as using an index. However its not "sorted". ;-)

I had to try it this morning, although the help says its a "sorted
collection" I find it behaves more like an "ordered collection".

The difference being I would expect a "sorted collection" to be always
sorted by the key, while an "ordered collection" is simply ordered by the
order I add key/value pairs to the collection.

Just a thought
Jay

"Bjorn Abelli" <bj**********@D oNotSpam.hotmai l.com> wrote in message
news:eN******** ******@TK2MSFTN GP12.phx.gbl...
"Pavils Jurjans" wrote...
The advantage to use some other class than ordinary
arrays for collections is that the size can be altered
"dynamicall y" by just adding new items.
I was thinking of ArrayList there


Well, both ArrayList as well as ordinary arrays implements the interface
ICollection.

*Which* implementation to choose is in many cases a choice of preference.
That makes me wonder why then there is the
NameValueCollec tion, which is also a collection
of string key-value pairs. It escapes me why should
I use speciffically the one or the other.


A NameValueCollec tion works in a slightly different manner, as it's

foremost a "sorted collection", which means that you can refer to a single item both through the key as well as using an index.

// Bjorn A

Nov 16 '05 #8
Jay B. Harlow wrote...

[On the topic of "NameValueColle ction"]
a "sorted collection", which means that you can
refer to a single item both through the key as
well as using an index.
However its not "sorted". ;-)


Well, don't blame me, blame the authors
of Microsoft's help... ;-)
Another odd thing is the results from:

foreach (string y in x)
{
Console.WriteLi ne(y);
}

....compared to:

for (int i = 0; i < x.Count; i++)
{
Console.WriteLi ne( x[i] );
}

....when x is a NameValueCollec tion.

The first one prints the *keys* while the second one prints the *values*.
It's the correct behaviour according the help, but it's not very
intuitive...
// Bjorn A
Nov 16 '05 #9
Bjorn,
Another odd thing is the results from:
foreach (string y in x)
for (int i = 0; i < x.Count; i++)
...when x is a NameValueCollec tion.
The first one prints the *keys* while the second one prints the *values*. I hadn't noticed that feature, as in my test I used the key for the value...
In my test I was a little surprised that the For Each returned a string
instead of a DictionaryEntry .

Jay

"Bjorn Abelli" <bj**********@D oNotSpam.hotmai l.com> wrote in message
news:u6******** ******@TK2MSFTN GP12.phx.gbl... Jay B. Harlow wrote...

[On the topic of "NameValueColle ction"]
a "sorted collection", which means that you can
refer to a single item both through the key as
well as using an index.

However its not "sorted". ;-)


Well, don't blame me, blame the authors
of Microsoft's help... ;-)
Another odd thing is the results from:

foreach (string y in x)
{
Console.WriteLi ne(y);
}

...compared to:

for (int i = 0; i < x.Count; i++)
{
Console.WriteLi ne( x[i] );
}

...when x is a NameValueCollec tion.

The first one prints the *keys* while the second one prints the *values*.
It's the correct behaviour according the help, but it's not very
intuitive...
// Bjorn A

Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3956
by: Matthew Hood | last post by:
Here's the situation. I am developing an ASP.NET web application. Most of my forms will be accessing a database (MS Access) for either record creation/deletion/updating or for list lookups. The problem I have is my boss constantly wants to revise table and field names and I am the one that has to make sure everthing works after the fact. What I want to do is create 2 classes. The first one is my "Record" wrapper class that will map...
9
9994
by: What-a-Tool | last post by:
Dim MyMsg Set MyMsg = server.createObject("Scripting.Dictionary") MyMsg.Add "KeyVal1", "My Message1" MyMsg.Add "KeyVal2", "My Message2" MyMsg.Add "KeyVal3", "My Message3" for i = 1 To MyMsg.Count Response.Write(MyMsg.Item(i)) next
4
6176
by: Martin Widmer | last post by:
Hi folks. I am using this collection class: Public Class ContentBlocksCollection Inherits DictionaryBase 'Object variables for attributes 'Attributes Default Public Property Item(ByVal nDBKey As Long) As ContentBlock Get
1
4517
by: Martin Widmer | last post by:
Hi Folks. When I iterate through my custom designed collection, I always get the error: "Unable to cast object of type 'System.Collections.DictionaryEntry' to type 'ContentObjects.ContentBlock'." The error occurs at the "For...Each" line if this method:
1
1624
by: sdunkerson | last post by:
I think this will be a good one for anyone who fancies themselves a wiz with manipulating data structure in vb.net. Imagine a Dictionary collection of Object "A". One of the properties of Object "A" is dictionaty collection of Object "B". Can I sort Object "A" using one of the properties within the collection of Objects "B"? Using the data structure below, a report has many lines, each line has many items, each item is made up of a...
5
18478
by: gmccallum | last post by:
I am trying to create a base class that contains a collection that MUST be able to be referenced by both a string key and a positional index. This collection MUST keep the items in the same order as they are added. In 2.0, is there a collection that would support this? The List<> works to keep the order but doesn't allow me to reference the items by key like the Dictionary<> collection does, but I don't think the Dictionary collection...
7
6141
by: bonk | last post by:
Hello I am acessing a Dictionary<TKey,TValuefrom multiple threads and often in a foreach loop. While I am within one of the foreach loops the other threads must not modify the collection itself since that would cause an exception in the foreach loop "foreach can not continue because the colelction was modifed". Now what is the least expensive and threadsafe way to make sure that no other threads modifies that collection. Since one of the...
5
4676
by: pamela fluente | last post by:
I have been posting this question with no success. I do not know if I am not being clear of the question is too difficult :-)) or unclear. It seems to me that the need to wrap a collection is quite common in real world programs. I am having problem to understand how I can wrap collections in System.Collections.Generic. For example I want to wrap a System.Collections.Generic.Dictionary. I wish the wrapping class to have a constructor...
7
57556
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain either/both of these collection types and then how do you convert or cast from the base to either a...
8
1998
by: Andy B | last post by:
I have the object property StockContract.Dictionary which is a dictionary collection of <string, stringkey/value pairs. I need to be able to retreive the keys and their values and display them on a page. I want to use something like a repeater or something like that. I don't know if this would be code I will have to manually write myself, or if it can be done with dataBinding of some sort. I am using vs2008 and c# 3.5. Any ideas on how to...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.