班级列表在控制台中作为班级名称继续打印?好吧,也许我只是累了或什么的,但我似乎无法弄清楚为什么会这样。每天为我拥有的数据库中的数据点调用以下代码。当我打印到控制台进行调试时,它只打印:NamespaceName.SharePrices不知道发生了什么。publicvoidOnData(TradeBars数据){十进制价格=数据["IBM"].Price;今天的DateTime=data["IBM"].Time;//----------------下面的工作正常。if(today.Date>=nextTradeDate.Date){MarketOnOpenOrder("IBM",50);Debug("购买的股票");nextTradeDate=today.AddDays(1);MarketOnOpenOrder("IBM",-25);}varderpList=newSharePrices{theDate=today,sharePrice=price};列表newList=newList();newList.Add(derpList);newList.ForEach(Console.WriteLine);}}publicclassSharePrices{publicDateTimetheDate{get;放;}publicdecimalsharePrice{get;放;请原谅我的命名约定。这只是个人项目的线框图。//–––––编辑感谢您的帮助。我想我不明白的是为什么它在我编写的TestClass中工作只是为了玩弄假数据,而当真正的实现出现时它不起作用:publicstaticvoidFindWindowDays(){vardates=newList();对于(vardt=currentDate.AddDays(-windowDays);dti);foreach(vardatesyoinascending){Console.WriteLine(datesyo);这似乎打印DateTime罚款控制台而不转换为字符串。但是当我添加第二个元素时它停止工作。这就是我感到困惑的地方。您应该根据需要以格式覆盖类的ToString(),例如:publicclassSharePrices{publicDateTimetheDate{get;放;}publicdecimalsharePrice{get;放;}publicoverridestringToString(){returnString.Format("TheDate:{0};SharePrice:{1};",theDate,sharePrice);默认情况下,没有覆盖,ToString()返回表示当前对象的字符串。这就是为什么你得到你所描述的。除了类名之外,C#不支持SharePrices。如果你想让它显示一些特定的东西,你需要像这样覆盖ToString()方法:}当然,你可以随意格式化,这就是它的妙处。如果您只关心价格而不关心日期,则只返回sharePrice。当您在某个类上调用Console.WriteLine时,它??会自动调用该类的ToString()方法。如果你想打印细节,你需要在你的类中覆盖ToString(),或者用你想打印的每个属性调用Console.WriteLine。这将工作而无需使用.ToString()以上是C#学习教程:类列表在控制台中作为类名不断打印?如果分享的内容对你有用,需要了解更多C#学习教程,希望你多多关注——publicclassSharePrices{publicDateTimetheDate{get;放;}publicdecimalsharePrice{get;放;}}SharePricessp=newSharePrices(){theDate=DateTime.Now,sharePrice=10};varnewList2=新列表();newList2.Add(sp);newList2.ForEach(itemX=>Console.WriteLine("日期:{0}Sharprice:{1}",sp.theDate,sp.sharePrice));本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
