In a previous post I described how to create an array with a custom data type we declared in a separate class.

This post will do the same and using the same custom class but now instead of creating an array we do the same with a generic List<T>.


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-1

C# | List Class
https://www.geeksforgeeks.org/c-sharp-list-class/

C# List
https://www.csharp-examples.net/list/