0

I have a hard time getting the value of an item of a class. I'm writing a method to reading CSV file. I binding the data which I need into a class. My class looks as follows:

public class DatafromCSV
{
    public string ImageId { get; set; }
    public List<string> ImageBbox { get; set; }
    public string Classname { get; set; }

}

This is my method to read the CSV file.

public static DatafromCSV ReadfromCSV (string path)
    {
        List<string> csvLines = new List<string>();            

        csvLines = File.ReadAllLines(path).ToList();
        csvLines = csvLines.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList();

        DatafromCSV currentRecord = new DatafromCSV();
        foreach (string line in csvLines)
        {
            string[] items = line.Split(',', '.');               

            currentRecord.ImageId = items[0];
            currentRecord.ImageBbox = new List<string>() { items[2], items[3], items[4], items[5] };
            currentRecord.Classname = items[6];

        }

        return currentRecord;
    }

I initialize my method as follows:

var infoCSV = ReadfromCSV(path);

After initializing the function I can access the ImageId and Classname values:

Console.WriteLine(infoCSV.ImageId);

However when I try to access the ImageBbox I getting following:

Console.WriteLine(infoCSV.ImageBbox);

System.Collections.Generic.List`1[System.String]

I expecting to get:

[2012,1367,2284,1473]

Is there a problem with how I calling it or in general in my approach? I have tried what I can but without any success. How can I get what I need?

linasster
  • 139
  • 8

2 Answers2

1

List<T> doesn't implement a custom ToString method override. So you get the default one, which gives you the type.

If you want to print out the elements in a list, it's as easy as using string.Join:

Console.WriteLine($"[{string.Join(",", infoCSV.ImageBbox)}]");
Luaan
  • 62,244
  • 7
  • 97
  • 116
1

You can use string.Join() to print list to the console.

Console.WriteLine($"[{string.Join(", ", infoCSV.ImageBbox)}]");

From MSDN: string.Join()

Concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member.

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44