By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,369 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,369 IT Pros & Developers. It's quick & easy.

Dictionary<> to List<>

P: n/a
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 dictionary or list?
thanks,

-Andy
Mar 8 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
How efficient is this?

Dictionary<EntityKey, Entitydictionary = new
Dictionary<EntityKey, Entity>();

// build the dictionary....

List<Entitylist = new List<Entity>(dictionary.Values);
"Andrew Robinson" <ne****@nospam.nospamwrote in message
news:eK*************@TK2MSFTNGP03.phx.gbl...
>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 dictionary or list?
thanks,

-Andy

Mar 8 '07 #2

P: n/a
Hi Ronbinson
"Andrew Robinson" <ne****@nospam.nospamschrieb im Newsbeitrag
news:eK*************@TK2MSFTNGP03.phx.gbl...
>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.
Why this has to be one method? The caller needs to know, what the method
returns, I'd suppose. So why not having to methods? Maybe an overload?
>
1. Is there any way to convert from a dictionary to a list without
itterating through the entire collection and building up a list?
No there isn't.
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 dictionary or list?
Well, they both implement ICollection and IEnumerable. But they would have
different Elementtypes (KeyValuePair<k,vresp. v). And surely both
implement object ;-), but I don't think, that's what you want.

Christof
Mar 8 '07 #3

P: n/a
On 8 Mar, 16:43, "Andrew Robinson" <nem...@nospam.nospamwrote:
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.
Dictionary.Values seems to behave in the same way as the List object,
e.g.

Dictionary<int, stringdict = new Dictionary<int,
string>();
foreach (string s in dict.Values)
{
}

List<stringlist = new List<string>();
foreach (string s in list)
{
}

[I was about to post this and then spotted your own reply... If they
behave as ecpected, then I guess it does boil down to efficiency -
have you done some timings?]

Mar 8 '07 #4

P: n/a
Andrew Robinson wrote:
How efficient is this?

Dictionary<EntityKey, Entitydictionary = new
Dictionary<EntityKey, Entity>();

// build the dictionary....

List<Entitylist = new List<Entity>(dictionary.Values);

If it doesn't absolutely have to be a List<Entity>, you can use:

ValueCollection<Entitylist = dictionary.Values;

or:

ICollection<Entitylist = dictionary.Values;

or:

IEnumerable<Entitylist = dictionary.Values;
ValueCollection<Timplements both ICollection<Tand IEnumerable<T>.
If you want a list, the constructor of the list that takes the
collection will just copy the elements of the collection into a new
list. If Entity is a class, this means that it just copies the references.

--
Göran Andersson
_____
http://www.guffa.com
Mar 8 '07 #5

P: n/a
thanks,

-A

"Göran Andersson" <gu***@guffa.comwrote in message
news:uJ**************@TK2MSFTNGP03.phx.gbl...
Andrew Robinson wrote:
>How efficient is this?

Dictionary<EntityKey, Entitydictionary = new
Dictionary<EntityKey, Entity>();

// build the dictionary....

List<Entitylist = new List<Entity>(dictionary.Values);


If it doesn't absolutely have to be a List<Entity>, you can use:

ValueCollection<Entitylist = dictionary.Values;

or:

ICollection<Entitylist = dictionary.Values;

or:

IEnumerable<Entitylist = dictionary.Values;
ValueCollection<Timplements both ICollection<Tand IEnumerable<T>.
If you want a list, the constructor of the list that takes the collection
will just copy the elements of the collection into a new list. If Entity
is a class, this means that it just copies the references.

--
Göran Andersson
_____
http://www.guffa.com
Mar 8 '07 #6

P: n/a

Sounds like a nightmare to maintain if the method can return a
dictionary in on situation or a list in another. Why not create two
methods wich different names then it's much clearer?

The List based on can call the Dictionary one and ither return
ICollection from the values or create a List depending on your needs.

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Thu, 8 Mar 2007 08:43:11 -0800, "Andrew Robinson"
<ne****@nospam.nospamwrote:
>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 dictionary or list?
thanks,

-Andy
Mar 8 '07 #7

P: n/a
Just an fyi to everyone: In the end, I created a method that takes a
delegate. Now you can create an anonymous method, pass it when you invoke
the method and create whatever collection type you want. Elegant solution!

"Samuel R. Neff" <sa********@nomail.comwrote in message
news:q4********************************@4ax.com...
>
Sounds like a nightmare to maintain if the method can return a
dictionary in on situation or a list in another. Why not create two
methods wich different names then it's much clearer?

The List based on can call the Dictionary one and ither return
ICollection from the values or create a List depending on your needs.

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Thu, 8 Mar 2007 08:43:11 -0800, "Andrew Robinson"
<ne****@nospam.nospamwrote:
>>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 dictionary or list?
thanks,

-Andy
Mar 12 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.