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.