Sander Bloem
Sander Bloem

Reputation: 15

C# how to check array in list

I'm building a Worker service for Windows who gets data from another program on the system. at this point i have all data that is need now i want to keep a list with up to date data. When i run the application for the Zones i get System.Int32[] what i would love to see is the data from System.Int32[]

how to obtain this?

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
            }) ;

My BroadcastModel class looks like the following:

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; }
}

Thanks in advance.

for testing purpose i added the following:

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("****************************");
        }

but then i got the system.int32[]

Upvotes: 1

Views: 82

Answers (1)

Prasad Telkikar
Prasad Telkikar

Reputation: 16049

Whenever you are printing data using Console.WriteLine(), it calls .ToString() method, if .ToString() is not overridden then it calls Object.ToString() method. Object.ToString() prints type in the string format.

In your case Console.WriteLine("Broadcast Zones: " + omroep.Zones); is printing System.Int32[], because it is calling ToString() method with the basic behavior.

To solve your issue, I would suggest to Override ToString() method in BroadcastModel class and return string which you want to print.

To print array elements, use string.Join() method.

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)}";
    }
}

Now you can print List<BroadcastModel> using foreach loop,

foreach(var broadcastmodel in activeOmroep)
   Console.WriteLine(broadcastmodel);

Upvotes: 2

Related Questions