简体   繁体   English

如何在C#中存储表-我应该使用哪种数据结构?

[英]How to store tables in C# - What kind of data structure should I use?

Here is how it looks generally: 一般情况如下:

I get values from an outside device and I have to store a timestamp with them. 我从外部设备获取值,并且必须与它们一起存储时间戳。 Somehow I want to store the info in an efficient way to make my life easier when it comes to processing all the stuff. 我想以某种有效的方式存储信息,以便在处理所有内容时使我的生活更轻松。

As a simple solution I have created a class which stores one row: 作为一个简单的解决方案,我创建了一个存储一行的类:

public class X 
{
  public DateTime timestamp;
  public double value1;
  public double value2;
}

And then I create a List out of these objects. 然后,我根据这些对象创建一个列表。

Can you show a better way of dealing with this kind of data? 您能否显示一种更好的处理此类数据的方法?

Thanks! 谢谢!

That seems perfectly reasonable to me, although I'd use properties rather than public fields. 尽管我会使用属性而不是公共字段,但这对我来说似乎完全合理。 If you're fetching values from an external device, you may want to consider making the type immutable - pass all the values into the constructor and store them in readonly fields. 如果要从外部设备获取值,则可能要考虑使类型不可变-将所有值传递到构造函数中并将它们存储在只读字段中。 I find it easier to reason about my code when the types are immutable :) 当类型不可变时,我发现对我的代码进行推理更容易:)

What sort of efficiency are you concerned about? 您关心哪种效率? How many of these values will you have? 您将拥有多少这些价值? Even though there aren't many values here, I'd probably still use a class instead of a struct... I very rarely create my own structs. 即使这里没有太多值,我仍可能会使用类而不是结构...我很少创建自己的结构。 Again, I just find classes easier to reason about (no need to worry about boxing, for example.) Also, this feels like "a collection of values" rather than one indivisible value which is what structs are usually used for (numbers etc). 再说一次,我只是发现类易于推理(例如,不必担心装箱。)而且,这感觉就像是“一组值”,而不是一个通常用于结构(数字等)的不可分值。 。 It just doesn't feel like a struct to me. 它只是不觉得自己像一个struct给我。

You will incur an overhead per object, but I wouldn't worry about that unless you have a really good reason to. 每个对象都会产生开销,但是除非您有充分的理由,否则我不会担心。

EDIT: This seems a trivial point to make, but give everything meaningful names. 编辑:这似乎很重要,但是给所有有意义的名称。 For example: 例如:

public class DeviceReading
{
    private readonly DateTime timeStamp;
    public DateTime TimeStamp { get { return timeStamp; } }

    private readonly double temperature;
    public double Temperature { get { return temperature; } }

    private readonly double airPressure;
    public double AirPressure { get { return airPressure; } }

    public DeviceReading (DateTime timeStamp,
                          double temperature,
                          double airPressure)
    {
        this.timeStamp = timeStamp;
        this.temperature = temperature;
        this.airPressure = airPressure;
    }
}

I suspect you'd do so anyway, but I just thought I'd make sure :) 我怀疑您还是会这样做,但我只是想确保自己可以:)

A couple of other things to consider: 还有几件事要考虑:

  • Assuming you're using a fairly recent version of .NET, you might want to use DateTimeOffset rather than DateTime . 假设您使用的是.NET的最新版本,则可能要使用DateTimeOffset而不是DateTime The benefit is that DateTiemOffset unambiguously represents an instant in time, whereas DateTime can be UTC, local or unspecified. 好处是DateTiemOffset明确表示一个时间点,而DateTime可以是UTC,本地或未指定。

  • Depending on the kind of readings you're taking (and how they're communicated) you may want to use decimal instead of double . 根据您要获取的读数类型(以及读数的传达方式),您可能希望使用decimal而不是double Probably not for temperature and air pressure (or any other "physical" values) but if you're dealing with artificial values like money, decimal is your friend. 可能不是温度和气压(或任何其他“物理”值)的原因,但是如果您要处理诸如金钱之类的人工值,则decimal是您的朋友。 Given that this is reading from a device, you're probably dealing with physical quantities, but I thought it worth mentioning. 鉴于这是从设备读取的,您可能正在处理物理量,但我认为值得一提。

  • For dealing with the collections of readings, you really want to look into LINQ. 为了处理阅读材料,您真的很想研究LINQ。 It's lovely :) 这很可爱:)

This type is small enough that, assuming it is immutable, you may like to consider using a struct. 此类型足够小,假设它是不可变的,您可能要考虑使用结构。 This will reduce the load on the garbage collected heap. 这将减少垃圾收集堆上的负载。

What exactly are you trying to optimise? 您到底想优化什么? Memory footprint? 内存占用量? Read performance? 阅读表现? How will you be reading the data? 您将如何读取数据?

If you plan to put many X object in an array, I suggest making it a struct since it holds just a few "plain old data" items (no objects). 如果您打算将多个X对象放入一个数组中,建议您将其构造为一个struct因为它仅包含一些“纯旧数据”项(无对象)。 Other than that, it seems like a straightforward way to work with the data. 除此之外,这似乎是处理数据的一种直接方法。

There is nothing particularly wrong with the way you are already doing it. 您已经做的方式没有什么特别的错误。 (Although as others have suggested, you should be making your fields private and using public properties instead) (尽管正如其他人所建议的那样,您应该将字段设为私有,而应使用公共属性)

Often the best way to decide upon what storage structure to use is to consider how you will be using the data. 通常,决定使用哪种存储结构的最佳方法是考虑如何使用数据。

For example, if you know you need to process the data in some kind of order, consider a SortedList instead. 例如,如果您知道需要按某种顺序处理数据,请考虑使用SortedList。 If you know you need direct access to the data from some kind of key or index, use a Dictionary. 如果您知道需要直接从某种键或索引访问数据,请使用字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM