C#如何检查列表中的数组
C# how to check array in list
我正在为 Windows 构建一个 Worker 服务,它从系统上的另一个程序获取数据。
在这一点上,我拥有所有需要的数据,现在我想保留一个包含最新数据的列表。
当我 运行 申请区域时,我得到 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
}) ;
我的 BroadcastModel
class 如下所示:
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("****************************");
}
但后来我得到了 system.int32[]
每当您使用 Console.WriteLine()
打印数据时,它都会调用 .ToString()
方法,如果 .ToString()
没有被覆盖,那么它会调用 Object.ToString()
方法。 Object.ToString()
以字符串格式打印类型。
在你的例子中 Console.WriteLine("Broadcast Zones: " + omroep.Zones);
正在打印 System.Int32[]
,因为它正在调用具有基本行为的 ToString()
方法。
为了解决您的问题,我建议在 BroadcastModel
class 和 return 您要打印的字符串中使用 Override
ToString()
方法。
要打印数组元素,请使用 string.Join()
方法。
Concatenates the elements of a specified array or the members of a
collection, using the specified separator between each element or
member.
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; }
public override string ToString()
{
return $"ID : {this.Id}, \nName: {this.Name} \nIs recording: {this.Recording} \nZones : {string.Join(", ", this.Zones)}";
}
}
现在您可以使用 foreach
循环打印 List<BroadcastModel>
,
foreach(var broadcastmodel in activeOmroep)
Console.WriteLine(broadcastmodel);
我正在为 Windows 构建一个 Worker 服务,它从系统上的另一个程序获取数据。
在这一点上,我拥有所有需要的数据,现在我想保留一个包含最新数据的列表。
当我 运行 申请区域时,我得到 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
}) ;
我的 BroadcastModel
class 如下所示:
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("****************************");
}
但后来我得到了 system.int32[]
每当您使用 Console.WriteLine()
打印数据时,它都会调用 .ToString()
方法,如果 .ToString()
没有被覆盖,那么它会调用 Object.ToString()
方法。 Object.ToString()
以字符串格式打印类型。
在你的例子中 Console.WriteLine("Broadcast Zones: " + omroep.Zones);
正在打印 System.Int32[]
,因为它正在调用具有基本行为的 ToString()
方法。
为了解决您的问题,我建议在 BroadcastModel
class 和 return 您要打印的字符串中使用 Override
ToString()
方法。
要打印数组元素,请使用 string.Join()
方法。
Concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member.
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; }
public override string ToString()
{
return $"ID : {this.Id}, \nName: {this.Name} \nIs recording: {this.Recording} \nZones : {string.Join(", ", this.Zones)}";
}
}
现在您可以使用 foreach
循环打印 List<BroadcastModel>
,
foreach(var broadcastmodel in activeOmroep)
Console.WriteLine(broadcastmodel);