【问题标题】:Class List Keeps Printing Out As Class Name In Console?班级列表在控制台中一直打印为班级名称?
【发布时间】: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


【解决方案1】:

您应该以您想要的格式为您的班级覆盖ToString(),例如:

public class SharePrices
{
    public DateTime theDate { get; set; }
    public decimal sharePrice { get; set; }

    public override string ToString()
    {
        return String.Format("The Date: {0}; Share Price: {1};", theDate, sharePrice);
    }
}

默认情况下,ToString() 在不覆盖的情况下返回一个表示当前对象的字符串。所以这就是为什么你得到你所描述的。

【讨论】:

    【解决方案2】:

    当您在一个类上调用Console.WriteLine 时,它会自动调用该类上的ToString() 方法。

    如果您想打印出详细信息,您将需要在课堂上覆盖 ToString(),或者使用您要打印的每个属性调用 Console.WriteLine

    【讨论】:

      【解决方案3】:

      除了类名之外,C# 对SharePrices 一无所知。如果您希望它显示特定内容,则需要像这样覆盖 ToString() 方法:

      public override string ToString()
      {
          return "SharePrice: " + theDate.ToString() + ": " + sharePrice.ToString();
      }
      

      当然,你可以随意格式化它,这就是它的美妙之处。如果您只关心价格而不关心日期,则只需return sharePrice

      【讨论】:

      • 嗯,我明白你在说什么。谁能想到一种方法来做到这一点而不转换为 SharePrices 类中的字符串?我需要稍后在另一个课程中使用这些数据。
      • @J.Rewo,您的数据不会更改。当您调用 .ToString() 时,您将获得新的字符串 obj,而不会更改您的类的状态。
      【解决方案4】:

      这无需使用.ToString()就可以工作

      public class SharePrices
      {
          public DateTime theDate { get; set; }
          public decimal sharePrice { get; set; }
      }
      
      SharePrices sp = new SharePrices() { theDate = DateTime.Now, sharePrice = 10 };
      
      var newList2 = new List<SharePrices>();
      newList2.Add(sp);
      newList2.ForEach(itemX => Console.WriteLine("Date: {0} Sharprice: {1}",sp.theDate, sp.sharePrice));
      

      【讨论】:

      • 我认为 foreach 正在等待 Action,但是你传递了 Action,所以这就是你得到编译错误的原因。我错了吗?
      • 这个编译得很好,不确定你在说什么,也许你不习惯看到这种类型的循环通过List&lt;T&gt;对象加载一个带有一些字符串的列表并尝试一下
      • dotnetfiddle.net/PlXwFe ,我说的是那个。看看问题,他正在循环 List.
      猜你喜欢
      • 1970-01-01
      • 2021-09-01
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 2019-04-26
      相关资源
      最近更新 更多