gpt4 book ai didi

C# 多变量数组

转载 作者:太空宇宙 更新时间:2023-11-03 18:36:28 25 4
gpt4 key购买 nike

在 C# 中有没有办法拥有一个包含多个变量的数组?

例如,我有一只股票的数据:

日期 |开盘价 |最高价 |最低价 |收盘价

10-01-2012| 10.00| 11.01| 9.56| 10.56

10-02-2012| 10.56| 10.99| 9.21| 9.99

10-03-2012| 9.99 | 10.12| 9.78| 10.11


我想做的是创建一个数组,它接受一个日期时间和一个字符串变量,并输出一个 double /字符串。

所以,如果我想获得 2012 年 1 月 1 日股票的开盘价,我可以说

DateTime Day = Convert.ToDateTime("10-01-2012");double openPrice = MyArray[Day,"Open"];

它会以 double 或字符串形式返回 10.00。

最好的方法是什么?这甚至可以用数组吗?如果没有,我可以使用哪些其他方法?我已经考虑了一段时间了,但我不确定构建这个数组/对象的最佳方式

感谢您的帮助!

最佳答案

创建一个类来包含您的数据并创建一个数组或这些类的列表可能更好;

class DailyPrice
{
DateTime Date { get; set; }
decimal Open { get; set; }
decimal Close { get; set; }
decimal High { get; set; }
decimal Low { get; set; }
}


static class Program
{
static void Main()
{
List<DailyPrice> prices = new List<DailyPrice>();
prices.Add(new DailyPrice() { Date = DateTime.Today, Open = 11.11M, Close=... });
prices.Add(new DailyPrice() { Date = DateTime.Today, Open = 12.14M, High=... });
...
}
}

顺便说一句,由于在 C# 中使用 double 类型执行算术运算时存在精度问题,用户 decimal 更安全地使用货币值(我假设您在这里拥有的是).

关于C# 多变量数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14533667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com