I am new to C# and I have been struggling to do the following: I'm trying to List a list in a console application, I have a model called "TeamModel"
public class TeamModel
{
public int Id { get; set; }
public string TeamName { get; set; }
public List<PersonModel> TeamMembers { get; set; } = new List<PersonModel>();
public TeamModel()
{
}
}
In my main class I have the following:
class Program
{
static void Main(string[] args)
{
List<TeamModel> TeamOne = new List<TeamModel>(){new TeamModel() { Id =1, TeamName = "x's Team", TeamMembers = null}};
List<TeamModel> TeamTwo = new List<TeamModel>(){new TeamModel() { Id =2, TeamName = "y's Team", TeamMembers = null}};
List<TeamModel> TeamThree = new List<TeamModel>(){new TeamModel() { Id =3, TeamName = "z's Team", TeamMembers = null}};
List<List<TeamModel>> listOfTeams = new List<List<TeamModel>> (){TeamOne,TeamTwo,TeamThree};
foreach (List<TeamModel> list in listOfTeams)
{
Console.WriteLine(list);
}
}
}
Now when I run the program I expect the result to be:
1,x's Team ,
2,y's Team ,
3,z's Team
Instead what I'm getting is
System.Collections.Generic.List`1[TeamModel]
System.Collections.Generic.List`1[TeamModel]
System.Collections.Generic.List`1[TeamModel]
If I change the foreach to :
foreach (List<TeamModel> list in listOfTeams)
{
Console.WriteLine(String.Join(", ", list));
}
I get this:
TeamModel
TeamModel
TeamModel