I am reading a CSV file in C# and creating Player objects from each line. The first line of the file is a header, so I wanted to skip it before processing the data.
When I use Skip(2), my program reads fewer players than I expect. I think I may be misunderstanding how Skip() works.
Why does Skip(2) skip too many lines here, and should I use Skip(1) instead if I only want to skip the header row?
Here is my full code:
namespace ConsoleApp1
{
public class Player
{
public string PlayerId { get; set; }
public string Name { get; set; }
public int BirthYear { get; set; }
public string Nationality { get; set; }
public string Position { get; set; }
public string ClubId { get; set; }
public string ClubName { get; set; }
public int ShirtNumber { get; set; }
public int Rating { get; set; }
public string StrongFoot { get; set; }
public int Height { get; set; }
public DateTime ContractStart { get; set; }
public DateTime ContractEnd { get; set; }
public int FormRating { get; set; }
public Player(string fajlsor)
{
string[] adat = fajlsor.Split(';');
PlayerId = adat[0];
Name = adat[1];
BirthYear = int.Parse(adat[2]);
Nationality = adat[3];
Position = adat[4];
ClubId = adat[5];
ClubName = adat[6];
ShirtNumber = int.Parse(adat[7]);
Rating = int.Parse(adat[8]);
StrongFoot = adat[9];
Height = int.Parse(adat[10]);
ContractStart = DateTime.Parse(adat[11]);
ContractEnd = DateTime.Parse(adat[12]);
FormRating = int.Parse(adat[13]);
}
}
internal class Program
{
public static List Players = new List();
static void Main(string[] args)
{
foreach (var item in File.ReadAllLines("arsenal_players.csv").Skip(2))
{
Players.Add(new Player(item));
}
Console.WriteLine("Players count: " + Players.Count);
var forwards = Players.Where(x => x.Position == "Forward");
foreach (var p in forwards)
{
Console.WriteLine(p.Name + " - " + p.Rating);
}
}
}
}
I expected all player rows except the header to be loaded, but one more row seems to be skipped.