Using a generic List with a custom data type in C#
brief, C#
In my previous post I described how to create an array with a custom data type we declared in a separate dedicated class.
In this post we will do the same and also using the same custom class, but this time instead of creating an array, we will use a generic List<T> for.
class Program
{
static void Main(string[] args)
{
List<CountryPopulation> countries = new List<CountryPopulation>()
{
new CountryPopulation() { Country = "Germany", Population = 83 },
new CountryPopulation() { Country = "USA ", Population = 328 },
new CountryPopulation() { Country = "France ", Population = 66 },
new CountryPopulation() { Country = "Spain ", Population = 46 },
};
foreach (var c in countries)
{
Console.WriteLine(c.Country + " " + "Population in m: " + c.Population);
}
Console.ReadLine();
}
}
class CountryPopulation
{
public string Country { get; set; }
public int Population { get; set; }
}Links
List<T> Class
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1C# | List Class
https://www.geeksforgeeks.org/c-sharp-list-class/
Tags In
Related Posts
Follow me on LinkedIn
