C#如何检查列表中的数组
我正在为Windows构建一个辅助服务,它从系统上的另一个程序获取数据。 在这一点上,我有所有需要的数据,现在我想保持一个最新的数据列表。 当我为区域运行应用程序时,我会得到C#如何检查列表中的数组,c#,list,model,C#,List,Model,我正在为Windows构建一个辅助服务,它从系统上的另一个程序获取数据。 在这一点上,我有所有需要的数据,现在我想保持一个最新的数据列表。 当我为区域运行应用程序时,我会得到System.Int32[]我希望看到的是System.Int32[] 如何获得这个 List<BroadcastModel> activeOmroep = new List<BroadcastModel>(); for (int o = 0; o < webcontent.Length; o
System.Int32[]
我希望看到的是System.Int32[]
如何获得这个
List<BroadcastModel> activeOmroep = new List<BroadcastModel>();
for (int o = 0; o < webcontent.Length; o++)
{
for (int i = 0; i < webcontent[o].Zones.Length; i++)
{
}
activeOmroep.Add(new BroadcastModel
{
Id = webcontent[o].Id,
Name = webcontent[o].Name,
Recording = webcontent[o].Recording,
Zones = webcontent[o].Zones
}) ;
提前谢谢
出于测试目的,我添加了以下内容:
public class BroadcastModel
{
public int Id { get; set; }
public string Name { get; set; }
public int[] Channels { get; set; }
public bool Recording { get; set; }
public int Type { get; set; }
public int Volume { get; set; }
public int[] Zones { get; set; }
}
foreach (var omroep in activeOmroep)
{
Console.WriteLine("Broadcast ID: " + omroep.Id);
Console.WriteLine("Broadcast Name: " + omroep.Name);
Console.WriteLine("Broadcast is recording: " + omroep.Recording);
Console.WriteLine("Broadcast Zones: " + omroep.Zones);
Console.WriteLine("****************************");
}
但是我得到了系统。int32[]每当您使用
控制台打印数据时。WriteLine()
,它调用.ToString()
方法,如果.ToString()
未被覆盖,则它调用对象.ToString()
方法Object.ToString()
以字符串格式打印类型
在您的例子中,Console.WriteLine(“广播区域:+omroep.Zones”)
正在打印System.Int32[]
,因为它正在用基本行为调用ToString()
方法
为了解决您的问题,我建议在BroadcastModel
类中重写ToString()
方法并返回要打印的字符串
要打印数组元素,请使用方法
连接指定数组的元素或
集合,在每个元素或元素之间使用指定的分隔符
成员
现在您可以使用foreach
循环打印List
foreach(var broadcastmodel in activeOmroep)
Console.WriteLine(broadcastmodel);
您在代码的哪一部分遇到了问题?@TonyStark我编辑了我的问题。。也许这会有帮助…太棒了,这很好用!谢谢大家!@SanderBloem,我更新了我的答案,让您更清楚地了解为什么要使用
System.Int32[]
foreach(var broadcastmodel in activeOmroep)
Console.WriteLine(broadcastmodel);