【发布时间】:2016-02-22 07:47:42
【问题描述】:
好的,所以也许我只是累了或其他什么,但我似乎无法弄清楚为什么会一直发生这种情况。
下面的代码每天都会为我拥有的数据库中的一个数据点调用。
当我打印到控制台进行调试时,它只是打印为:
NamespaceName.SharePrices
不知道发生了什么。
public void OnData(TradeBars data)
{
decimal price = data["IBM"].Price;
DateTime today = data["IBM"].Time;
//--------------Below works fine.
if (today.Date >= nextTradeDate.Date)
{
MarketOnOpenOrder("IBM", 50);
Debug("Purchased Stock");
nextTradeDate = today.AddDays(1);
MarketOnOpenOrder("IBM", -25);
}
var derpList = new SharePrices { theDate = today, sharePrice = price };
List<SharePrices> newList = new List<SharePrices>();
newList.Add(derpList);
newList.ForEach(Console.WriteLine);
}
}
public class SharePrices
{
public DateTime theDate { get; set; }
public decimal sharePrice { get; set; }
}
请原谅我的命名约定。这只是个人项目的线框图。
//---------编辑
感谢各位的帮助。我想我不明白的是为什么它在我编写的 TestClass 中工作,只是在玩假数据,而当真正的实现到来时它却不起作用:
public static void FindWindowDays()
{
DateTime currentDate = DateTime.Now;
var dates = new List<DateTime>();
for (var dt = currentDate.AddDays(-windowDays); dt <= currentDate; dt = dt.AddDays(1))
{
dates.Add(dt);
}
var ascending = dates.OrderByDescending(i => i);
foreach (var datesyo in ascending)
{
Console.WriteLine(datesyo);
}
}
这似乎可以很好地将 DateTime 打印到控制台而不转换为字符串。但是当我添加第二个元素时,它停止了工作。这就是我糊涂的地方。
【问题讨论】:
-
如果你想让它显示更具体的东西,你必须覆盖
ToString()。 -
您能否澄清Object.ToString 的哪一部分让您感到困惑? “
Object.ToString方法的默认实现返回对象类型的完全限定名称”......或者WriteLine 没有提到“调用值的 ToString 方法来生成其字符串表示”? -
这很令人困惑,因为在我的 TestClass 中,我用虚拟数据编写了它,它使用 DateTime 和 Decimal 打印到控制台就好了。我通常不在控制台中调试。
-
它在你的虚拟类中工作的原因是因为内置的
DateTime类有它自己的ToString()方法。您的自定义类没有,因此如果未指定任何内容,C# 默认为NamespaceName.ClassName。
标签: c# list multidimensional-array