473,795 Members | 3,041 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
11 18710
Hello John,
Wrong. Collections only need to be collections of values - no need for
keys.


I am confused about this denial... Say, NameValueCollec tion class. It's a
collection, right? But there's a key-and-value information inthere, true?

The following code writes all the *keys* withing the NameValueCollec tion:

foreach (string y in x)
{
Console.WriteLi ne(y);
}
As Jay comments down there in this thread, "...I was a little surprised that
the For Each returned a string instead of a DictionaryEntry .". Would that be
a collection, or as it was defined, "a pile of stuff, not a list of keys and
corresponding values", it would consist of key-value pair objects. Or is
that just a specific implementation of NameValueCollec tion enumerator here?

Confused,

Pavils
Nov 16 '05 #11
Pavils Jurjans <pa****@mailbox .riga.lv> wrote:
Wrong. Collections only need to be collections of values - no need for
keys.
I am confused about this denial... Say, NameValueCollec tion class. It's a
collection, right? But there's a key-and-value information inthere, true?


Yes. Just because there's I said there's no *need* for a collection to
have keys doesn't mean there *can't* be keys.

Proof of my statement is pretty simple though - ArrayList implements
ICollection, and there are no keys in that. Hence a class doesn't need
any concept of "key" to be a collection.
The following code writes all the *keys* withing the NameValueCollec tion:

foreach (string y in x)
{
Console.WriteLi ne(y);
}
As Jay comments down there in this thread, "...I was a little surprised that
the For Each returned a string instead of a DictionaryEntry .". Would that be
a collection, or as it was defined, "a pile of stuff, not a list of keys and
corresponding values", it would consist of key-value pair objects. Or is
that just a specific implementation of NameValueCollec tion enumerator here?


It's actually the NameObjectColle ctionBase's implementation of
GetEnumerator - and while it's surprising in that other mapping
collections act as a collection of DictionaryEntri es in this sense, the
documentation for NameObjectColle ctionBase.GetEn umerator does spell out
what it does:

<quote>
This enumerator returns the keys of the collection as strings.
</quote>

--
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 #12

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

Similar topics

1
3957
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
6177
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
4518
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
9672
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
9519
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
10438
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...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
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...
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.