473,408 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Nested Generic Dictionary Declaration

I am trying to create a nested Dictionary and get an error that seems odd to
me. Here is my declaration:

private IDictionary<Guid, IDictionary<Guid, string>>
myNestedDictionary
= new Dictionary<Guid, Dictionary<Guid, string>>();

I get an error unless I nest a Dictionary and not an IDictionary:

Error 3 Cannot implicitly convert type
'System.Collections.Generic.Dictionary<System.Guid ,System.Collections.Generic.Dictionary<System.Guid ,string>>'
to
'System.Collections.Generic.IDictionary<System.Gui d,System.Collections.Generic.IDictionary<System.Gu id,string>>'.
An explicit conversion exists (are you missing a
cast?) C:\Projects\DemoProject\Presenter\View\IncidentVie wState.cs 29 15 Presenter

So this works:
private IDictionary<Guid, Dictionary<Guid, string>>
myNestedDictionary
= new Dictionary<Guid, Dictionary<Guid, string>>();

I would rather deal with the interface but I am forced to the implentation.

Thanks,

David Morris
Nov 25 '07 #1
2 6269
What you mean is:

private IDictionary<Guid, IDictionary<Guid, string>>
myNestedDictionary
= new Dictionary<Guid, IDictionary<Guid, string>>();

i.e. a concrete implementation of a dictionary between guid and an
abstract dictionary - then you might use:
myNestedDictionary.Add(someGuid, new Dictionary<Guid, string>());

As a side note; since this is private there isn't much harm using the
concrete name anyway...

Marc
Nov 25 '07 #2
David Morris <dm******@newsgroup.nospamwrote:
I am trying to create a nested Dictionary and get an error that seems odd to
me.
You're basically running against the lack of variance in generics in
C#. It's exactly the same as trying:

List<objecto = new List<string>();

Consider what happens with the above if you write:

o.Add(new object());

Or indeed in your case, if you tried

myNestedDictionary[Guid.Empty] = new SomeOtherIDictionaryImpl();
There's considerable discussion about whether it should be supported
where safe for future versions though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Nov 25 '07 #3

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

Similar topics

4
by: Edward Diener | last post by:
Version 2.0 of the Python database API was written over 5 years ago, in 1999. While it has been used successfully by many implementations, there is no generic access into the data dictionary of...
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
2
by: Jasper Kent | last post by:
I'm trying to do the equivalent of using typedefs with templates in C++ to avoid long instantiation names. I can do this okay: using BigDictionary = System.Collections.Generic.Dictionary<int,...
7
by: Bill Woodruff | last post by:
I've found it's no problem to insert instances of named delegates as values into a generic dictionary of the form : private Dictionary<KeyType, DelegatemyDictionary = new Dictionary<KeyType,...
4
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of...
3
by: fstorck | last post by:
Hi, I'm kind of stuck with an serializing / deserializing problem using a generic dictionary holding references to various generic types. It goes as follows: <code> class MyBase :...
3
by: BombDrop | last post by:
I'm working on a system that has 4 different types of client they have mostly the same attributes so I decided to create an interface IClient for them to implement and then just add the differing...
2
by: Stephen Costanzo | last post by:
I have: Public Class test Private displayValue As String Private dbType As Integer Property Display() As String Get Return displayValue End Get
0
by: DaTurk | last post by:
Hi, I have a generic dictionary defined, and when I attpet to return a reference to it via a property I get this error error C3073: 'System::Collections::Generic::Dictionary<TKey,TValue>' : ref...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...
0
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...
0
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...

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.