123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
using System; using System.Collections; using System.Collections.Generic; // << add using System.Linq; using System.Text; using System.Threading.Tasks; namespace ListGenerics1 { class Program { static void Main(string[] args) { UseGenericsList(); Console.ReadLine(); } public static void UseGenericsList() { List <Person> people = new List<Person>() { new Person { Age = 123, FirstName="123", LastName="123" } }; // Count Console.WriteLine("Items in list: {0}",people.Count); //Enumerate foreach (Person p in people) { Console.WriteLine(p); } // Foreach... people.Add(new Person { Age = 123, FirstName = "123", LastName = "123" }); // Person[] arrayOfPeople = people.ToArray(); for (int i = 0; i < arrayOfPeople.Length; i++) { Console.WriteLine("First Names: {0}", arrayOfPeople[i].FirstName); } } } public class Person { public int Age{get;set;} public string FirstName { get; set; } public string LastName { get; set; } public Person() { } public Person(int age, string firstName, string lastName) { Age = age; FirstName = firstName; LastName = lastName; } } } |