PC Review


Reply
Thread Tools Rate Thread

Converting a Dictionary to an array?

 
 
Gustaf
Guest
Posts: n/a
 
      18th Dec 2006
This is two questions in one really. First, I wonder how to convert the
values in a Dictionary to an array. Here's the dictionary:

private Dictionary<Uri, Schema> schemas = new Dictionary<Uri, Schema>();

Uri is the System.Uri class, and Schema is a class I made. Now, I want
the class where this Dictionary is contained to have a property that
returns all the values in this Dictionary. Like such:

public Schema[] Schemas

How do I implement this property? I can figure out how to do it, but
that involves looping through all the elements in the dictionary, and
that's probably not the way it's meant to be done. I assume there must
be a one-liner solution, like the ArrayList.ToArray() method. I found
the Dictionary.Values property, but can't figure out how to make that
into an array.

My second question is about best practices. I wonder if it makes sense
to have a property that returns an array of Schema objects, rather than
returning another kind of collection? I want a basic and general
interface outwards, something that doesn't assume too much in terms of
what packages are used in the calling class. Then, is an object array
the best choice?

Gustaf
 
Reply With Quote
 
 
 
 
Dave Sexton
Guest
Posts: n/a
 
      18th Dec 2006
Hi Gustaf,

> This is two questions in one really. First, I wonder how to convert the
> values in a Dictionary to an array. Here's the dictionary:
>
> private Dictionary<Uri, Schema> schemas = new Dictionary<Uri, Schema>();
>
> Uri is the System.Uri class, and Schema is a class I made. Now, I want the
> class where this Dictionary is contained to have a property that returns
> all the values in this Dictionary. Like such:
>
> public Schema[] Schemas
>
> How do I implement this property? I can figure out how to do it, but that
> involves looping through all the elements in the dictionary, and that's
> probably not the way it's meant to be done. I assume there must be a
> one-liner solution, like the ArrayList.ToArray() method. I found the
> Dictionary.Values property, but can't figure out how to make that into an
> array.


public Schema[] Schemas
{
get
{
Schema[] array = new Schema[schemas.Count];
schemas.Values.CopyTo(array, 0);
return array;
}
}

> My second question is about best practices. I wonder if it makes sense to
> have a property that returns an array of Schema objects, rather than
> returning another kind of collection? I want a basic and general interface
> outwards, something that doesn't assume too much in terms of what packages
> are used in the calling class. Then, is an object array the best choice?


That depends on what the caller will be doing with the return value, but
most likely an array would be your best choice since it only represents a
copy of the values in your dictionary. Any other return type might be
misleading.

--
Dave Sexton


 
Reply With Quote
 
Jon Shemitz
Guest
Posts: n/a
 
      18th Dec 2006
Gustaf wrote:

> public Schema[] Schemas
>
> How do I implement this property? I can figure out how to do it, but
> that involves looping through all the elements in the dictionary, and
> that's probably not the way it's meant to be done. I assume there must
> be a one-liner solution, like the ArrayList.ToArray() method. I found
> the Dictionary.Values property, but can't figure out how to make that
> into an array.


You can do this as a one-liner, but it's not pretty:

return new List<Schema>(schemas.Values).ToArray();

If you don't want to get fired after your next code review, you
probably ought to break this into two statements:

List<Schema> Values = new List<Schema>(schemas.Values);
return Values.ToArray();

> My second question is about best practices. I wonder if it makes sense
> to have a property that returns an array of Schema objects, rather than
> returning another kind of collection? I want a basic and general
> interface outwards, something that doesn't assume too much in terms of
> what packages are used in the calling class. Then, is an object array
> the best choice?


Probably not. The approach that makes the fewest assumptions and
wastes the fewest cycles is to just expose a

public IEnumerable<Schema> Values
{
get { return schemas.Values; }
}

Then, if your callers just want to enumerate, they can do so without
any waste of time or memory. If they want a List<Schema>, they can
easily generate one; ditto if they want a Schema[].

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
 
Reply With Quote
 
Lucian Wischik
Guest
Posts: n/a
 
      18th Dec 2006
Gustaf <(E-Mail Removed)> wrote:
>I found the Dictionary.Values property, but can't figure out how to make that
>into an array.
>My second question is about best practices. I wonder if it makes sense
>to have a property that returns an array of Schema objects, rather than
>returning another kind of collection? I want a basic and general
>interface outwards, something that doesn't assume too much in terms of
>what packages are used in the calling class. Then, is an object array
>the best choice?


I don't think an object array is the best choice. Dictionary.Values
seems perfect for what you need. And I think it's more elegant for
objects to return ICollection<T> or IEnumerable<T> rather than T[].
(e.g. returning an IEnumerable<T> would let you completely rewrite
your code for returning the thing, perhaps even using the wonderful
"yield return").

--
Lucian
 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      18th Dec 2006
"Jon Shemitz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> You can do this as a one-liner, but it's not pretty:
>
> return new List<Schema>(schemas.Values).ToArray();
>
> If you don't want to get fired after your next code review


I've never understood that attitude at all - there is absolutely *nothing*
wrong with that single line of code...


 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      18th Dec 2006
Hi Mark,

Well, it's iterating the entire collection to copy it into a new object,
just so it can do another copy internally again to return it as an array,
simply to save 2 lines of code

--
Dave Sexton

"Mark Rae" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Jon Shemitz" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> You can do this as a one-liner, but it's not pretty:
>>
>> return new List<Schema>(schemas.Values).ToArray();
>>
>> If you don't want to get fired after your next code review

>
> I've never understood that attitude at all - there is absolutely *nothing*
> wrong with that single line of code...
>



 
Reply With Quote
 
Jon Shemitz
Guest
Posts: n/a
 
      18th Dec 2006
Mark Rae wrote:

> > You can do this as a one-liner, but it's not pretty:
> >
> > return new List<Schema>(schemas.Values).ToArray();
> >
> > If you don't want to get fired after your next code review

>
> I've never understood that attitude at all - there is absolutely *nothing*
> wrong with that single line of code...


It's sort of hard to parse, especially if you're a bit unclear on C#
operator precedence. And, imho, you can be a perfectly fine C#
programmer - comfortable with all the operators and idioms - and still
be a bit unclear on C# operator precedence. I mean **15** levels of
precedence? It makes a lot of sense to ignore that and use
"unnecessary" parens.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
 
Reply With Quote
 
Jon Shemitz
Guest
Posts: n/a
 
      18th Dec 2006
Dave Sexton wrote:

> Well, it's iterating the entire collection to copy it into a new object,
> just so it can do another copy internally again to return it as an array,
> simply to save 2 lines of code


Yeah - I was typing under the influence of the impression that Keys
and Values were IEnumerable<T>s, not collections. (It certainly seems
like that would be a better design.)

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
 
Reply With Quote
 
Fabio Z
Guest
Posts: n/a
 
      18th Dec 2006

"Dave Sexton" <dave@jwa[remove.this]online.com> ha scritto nel messaggio
news:(E-Mail Removed)...
> Hi Mark,
>
> Well, it's iterating the entire collection to copy it into a new object,
> just so it can do another copy internally again to return it as an array,
> simply to save 2 lines of code


Premised that I also prefere the second solution, I really don't see
difference into generated IL.
I would be more worried about the fact that a class shouldn't return a
Something<T> class to the extern.



 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      18th Dec 2006
"Fabio Z" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...

> Premised that I also prefere the second solution,


That's perfectly fine, but a very different argument...:-)

> I really don't see difference into generated IL.


That's because there isn't any, AFAICS

> I would be more worried about the fact that a class shouldn't return a
> Something<T> class to the extern.


:-)


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting byte array to short array? Ole Microsoft C# .NET 2 8th Jul 2006 01:28 AM
Problem converting managed array (C++) to C# array Dick Swager Microsoft C# .NET 2 30th Aug 2005 02:53 AM
Problem converting managed array (C++) to C# array Dick Swager Microsoft VC .NET 2 30th Aug 2005 02:53 AM
Converting a double array to byte array =?Utf-8?B?UmFncw==?= Microsoft Dot NET Framework 5 10th Aug 2005 01:22 PM
converting an array of bytes to an array of chars Claire Microsoft C# .NET 1 8th Jun 2004 04:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:35 AM.