如何存储在C#中的表 - 我应该使用什么样的数据结构? [英] How to store tables in C# - What kind of data structure should I use?

查看:270
本文介绍了如何存储在C#中的表 - 我应该使用什么样的数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一般的外观:

我从一个外部设备的价值观和我要存储一个时间戳他们。不知怎的,我想存储在一个有效的方式信息,使我的生活更轻松,当涉及到处理所有的东西。

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?

谢谢!

推荐答案

这似乎是完全合理的我,虽然我会使用属性而不是公共领域。如果从外部设备获取的值,你可能要考虑的类型不变的 - 所有的值传递到构造函数,并将它们存储在只读域。我觉得更容易来思考我的代码时,类型是不可改变的:)

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.

您将承担每个对象的开销,但我不会担心除非你有一个很好的理由。

You will incur an overhead per object, but I wouldn't worry about that unless you have a really good reason to.

编辑:这似乎是一个简单的点进行,但付出一切有意义的名称。例如:

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:


  • 假设你正在使用一个相当新的.NET版本,您可能需要使用的DateTimeOffset ,而不是的DateTime 。这样做的好处是, DateTiemOffset 明确表示的时间瞬间,而的DateTime 可以是UTC,本地或指定。

  • Assuming you're using a fairly recent version of .NET, you might want to use DateTimeOffset rather than DateTime. The benefit is that DateTiemOffset unambiguously represents an instant in time, whereas DateTime can be UTC, local or unspecified.

根据那种你服用(以及他们是如何沟通),您可能希望使用读数小数而不是双击。也许不是对温度和气压(或任何其他的物理的值),但如果你处理像金钱的人工值,小数是你的朋友。鉴于这是从设备读取,你的可能的处理的物理量,但我认为它值得一提的。

Depending on the kind of readings you're taking (and how they're communicated) you may want to use decimal instead of 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. Given that this is reading from a device, you're probably dealing with physical quantities, but I thought it worth mentioning.

对于处理读数的收藏,您的真正的想看看LINQ。它的可爱:)

For dealing with the collections of readings, you really want to look into LINQ. It's lovely :)

这篇关于如何存储在C#中的表 - 我应该使用什么样的数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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