将字典转换为数组? [英] Converting a Dictionary to an array?

查看:113
本文介绍了将字典转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真是两个问题。首先,我想知道如何将Dictionary中的

值转换为数组。这里是字典:


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


Uri is System.Uri类,Schema是我创建的类。现在,我希望

包含此Dictionary的类具有一个属性

返回此Dictionary中的所有值。像这样:


公共架构[]架构


如何实现此属性?我可以弄清楚如何去做,但是

涉及循环遍历字典中的所有元素,并且

这可能不是它的方式意味着要完成。我假设必须有一个单行解决方案,比如ArrayList.ToArray()方法。我在Dictionary.Values属性中找到了

,但是无法弄清楚如何将

变成数组。


我的第二个问题是关于最佳做法。我想知道是否有意义

有一个属性返回一个Schema对象数组,而不是

返回另一种类型的集合?我想要一个基本的和一般的

接口向外,在

的调用类中使用什么包的方面并没有太多假设。那么,对象数组是否是
的最佳选择?


古斯塔夫

解决方案

< blockquote>嗨古斯塔夫,


这真是两个问题。首先,我想知道如何将Dictionary中的

值转换为数组。这里是字典:


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


Uri is System.Uri类,Schema是我创建的类。现在,我希望包含此Dictionary的

类具有一个属性,该属性返回

此Dictionary中的所有值。像这样:


公共架构[]架构


如何实现此属性?我可以弄清楚如何做到这一点,但是

涉及循环遍历字典中的所有元素,而且'

可能不是它的方式意味着要完成。我假设必须有一个

单线解决方案,比如ArrayList.ToArray()方法。我找到了

Dictionary.Values属性,但无法弄清楚如何将它变成一个

数组。



public Sc​​hema [] Schemas

{

get

{

Schema [] array = new Schema [schemas.Count];

schemas.Values.CopyTo(array,0);

返回数组; < br $>
}

}


我的第二个问题是关于最佳实践。我想知道是否有意义

有一个返回Schema对象数组的属性,而不是

返回另一种类型的集合?我想要一个基本的和通用的界面

向外,对于在调用类中使用什么包

而言,这并不算太多。那么,对象数组是最佳选择吗?



这取决于调用者将使用返回值做什么,但是

最有可能数组是你最好的选择,因为它仅表示字典中值的副本
。任何其他返回类型可能是

误导。


-

Dave Sexton


Gustaf< gu ***** @ algonet.sewrote:


>我找到了Dictionary.Values属性,但不能弄清楚如何将它变成阵列。
我的第二个问题是关于最佳实践。我想知道是否有一个返回Schema对象数组的属性是否有意义,而不是返回另一种类型的集合?我想要一个基本的和一般的
接口向外,在调用类中使用什么包的方面并没有太多假设。那么,对象阵列是最好的选择吗?



我不认为对象数组是最佳选择。 Dictionary.Values

似乎非常适合您的需求。我认为

对象更优雅地返回ICollection< Tor IEnumerable< Trather而不是T []。

(例如返回IEnumerable< Twould让你彻底重写

你的代码返回的东西,甚至可能使用精彩的

" yield return")。


-

Lucian


Gustaf写道:


public Sc​​hema [] Schemas


如何实现此属性?我可以弄清楚如何去做,但是

涉及循环遍历字典中的所有元素,并且

这可能不是它的方式意味着要完成。我假设必须有一个单行解决方案,比如ArrayList.ToArray()方法。我在Dictionary.Values属性中找到了

,但是无法弄清楚如何将

变成数组。



你可以做一个单行,但它不漂亮:


返回新列表< Schema>(schemas.Values)。ToArray();


如果您在下次代码审核后不想被解雇,那么

可能应该将其分为两个陈述:


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

return Values.ToArray();


我的第二个问题是关于最佳实践。我想知道是否有意义

有一个属性返回一个Schema对象数组,而不是

返回另一种类型的集合?我想要一个基本的和一般的

接口向外,在

的调用类中使用什么包的方面并没有太多假设。那么,对象数组是否是
的最佳选择?



可能不是。使得最少的假设和

的方法浪费最少的周期就是暴露一个公共IEnumerable< SchemaValues

{


get {return schemas.Values; }

}


然后,如果您的来电者只想枚举,他们可以在没有任何浪费时间的情况下这样做记忆。如果他们想要一个List< Schema>,他们可以很容易地生成一个;同上,如果他们想要一个Schema []。


-


..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net

你需要知道什么。


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, Schemaschemas = 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

解决方案

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, Schemaschemas = 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


Gustaf <gu*****@algonet.sewrote:

>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<Tor IEnumerable<Trather than T[].
(e.g. returning an IEnumerable<Twould let you completely rewrite
your code for returning the thing, perhaps even using the wonderful
"yield return").

--
Lucian


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<SchemaValues = 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<SchemaValues
{
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.


这篇关于将字典转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆