什么集合对象适合值的固定顺序?
场景:我正在跟踪多个性能计数器,并且有一个与 DataSnapshot[] 相关的 CounterDescription[]...,其中 CounterDescription[n] 描述了 DataSnapshot[n] 中加载的数据。
我想在 C# 中公开一个易于使用的 API,以便轻松高效地扩展数组。
简化的示例(它变得更加复杂)
CounterDescription[0] = Humidity;
DataSnapshot[0] = .9;
CounterDescription[1] = Temp;
DataSnapshot[1] = 63;
请注意,我的意图是将许多数据快照与 DateTime
引用相关联,并使用数据的偏移量来引用其含义。这被确定为在后端存储数据的最有效方式,现在已反映为以下结构:
public class myDataObject {
[DataMember]
public SortedDictionary<DateTime, float[]> Pages { get; set; }
/// <summary>
/// An array that identifies what each position in the array is supposed to be
/// </summary>
[DataMember]
public CounterDescription[] Counters { get; set; }
}
如何使用 myDataObject?:
我会经常搜索计数器通过字符串名称,并使用其位置来确定将保存特定值的偏移量。我可以使用自行开发的扩展方法来枚举对象,或者在保证顺序的情况下利用框架。
另外,当添加新传感器时,我需要扩展每个数组:(float[]
和 CounterDescription[]
),但无论已存在的数据都必须保留在该数组中相对偏移量。我不希望该对象的序列化版本将 Temp(偏移量 1)与 Humidity(偏移量 0)混淆。
哪些 .NET 对象支持这种固定排序、扩展和枚举(以及可选的按字符串搜索)?我的猜测是使用这些对象之一......
Array[] , LinkedList<t>, and List<t>
Scenario: I am tracking several performance counters and have a CounterDescription[] correlate to DataSnapshot[]... where CounterDescription[n] describes the data loaded within DataSnapshot[n].
I want to expose an easy to use API within C# that will allow for the easy and efficient expansion of the arrays.
Simplified example (it gets more complex)
CounterDescription[0] = Humidity;
DataSnapshot[0] = .9;
CounterDescription[1] = Temp;
DataSnapshot[1] = 63;
Note how my intent is to correlate many Datasnapshots with a DateTime
reference, and using the offset of the data to refer to its meaning. This was determined to be the most efficient way to store the data on the back-end, and has now reflected itself into the following structure:
public class myDataObject {
[DataMember]
public SortedDictionary<DateTime, float[]> Pages { get; set; }
/// <summary>
/// An array that identifies what each position in the array is supposed to be
/// </summary>
[DataMember]
public CounterDescription[] Counters { get; set; }
}
How will myDataObject be used?:
I will frequently search for a counter by string name, and use its' position to determine what offset a particular value will be saved. I can use an homegrown extension method to enumerate the object, or leverage the framework if ordering is guaranteed.
Also, I will need to expand each of these arrays as new sensors are added: (float[]
and CounterDescription[]
), but whatever data already exists must stay in that relative offset. I don't want the serialized version of this object to confuse Temp (offset 1) with Humidity (offset 0)
Which .NET objects support this fixed ordering, expansion, and enumeration (and optional searching by string)? My guess is to use one of these objects...
Array[] , LinkedList<t>, and List<t>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
Dictionary
以便每个名称 (string
) 映射到一个值 (double
):并使用获取的服务并设置计数器值:
您可以以相同的方式使用
CounterDescription
和/或DataSnapshot
类,但请确保用作键的类(可能是>CounterDescription
) 使用正确的实现覆盖Object.Equals()
和Object.GetHashCode()
。Use a
Dictionary<string, double>
so that each name (string
) maps to a value (double
):And use a service that gets and sets the counter values:
You can use your
CounterDescription
and/orDataSnapshot
classes in the same way, but make sure that the class you use as the key (probablyCounterDescription
) overridesObject.Equals()
andObject.GetHashCode()
with a proper implementation.如果
CounterDescription
是一个字符串或一个enum
,那么Dictionary
似乎很合适:您可以使用
做同样的事情>enum
作为键,而不是字符串。如果您的
CounterDescription
类型是一个复杂对象,您仍然可以将其用作键,但您需要实现IComparable
或提供比较函数。If
CounterDescription
is a string or anenum
, thenDictionary
seems like a good fit:You can do the same sort of thing with a
enum
for the key, rather than a string.If your
CounterDescription
type is a complex object, you can still use it as a key, but you'll need to implementIComparable
or provide a comparison function.任何列表 (IList) 都有有序值。我总是假设 IEnumerables 下面没有严格的顺序;虽然他们通常会这样做,但你不能保证。我同意其他人的观点,即字典(或其他一些 IDictionary)很合适。
Any list (IList) has ordered values. I always assume that mere IEnumerables have no strict order underneath; although they usually do, you can't guarantee it. I agree with the others that a Dictionary (or some other IDictionary) is a good fit.