Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C# Programming

Dictionary<> to List<>


I have a method that needs to return either a Dictionary<k,v> or 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

How efficient is this?

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

            // build the dictionary....

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

"Andrew Robinson" <nem@nospam.nospam> wrote in message

news:eKxs4EaYHHA.688@TK2MSFTNGP03.phx.gbl...

Hi Ronbinson
"Andrew Robinson" <nem@nospam.nospam> schrieb im Newsbeitrag
news:eKxs4EaYHHA.688@TK2MSFTNGP03.phx.gbl...
>I have a method that needs to return either a Dictionary<k,v> or 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,v> resp. v). And surely both
implement object ;-), but I don't think, that's what you want.

Christof

-----------------------------------------------Reply-----------------------------------------------

On 8 Mar, 16:43, "Andrew Robinson" <nem@nospam.nospam> wrote:

> I have a method that needs to return either a Dictionary<k,v> or 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, string> dict = new Dictionary<int,
string>();
            foreach (string s in dict.Values)
            {
            }

            List<string> list = 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?]

-----------------------------------------------Reply-----------------------------------------------

Andrew Robinson wrote:
> How efficient is this?

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

>            // build the dictionary....

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

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

ValueCollection<Entity> list = dictionary.Values;

or:

ICollection<Entity> list = dictionary.Values;

or:

IEnumerable<Entity> list = dictionary.Values;

ValueCollection<T> implements both ICollection<T> and 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.

--
Gran Andersson
_____
http://www.guffa.com

-----------------------------------------------Reply-----------------------------------------------

thanks,

-A

"Gran Andersson" <g@guffa.com> wrote in message

news:uJvng8aYHHA.1220@TK2MSFTNGP03.phx.gbl...

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"

 
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc