Lister er kult

This commit is contained in:
Geir Okkenhaug Jerstad 2024-11-25 14:07:06 +01:00
parent 12c164c6b3
commit f186fcf653
2 changed files with 11 additions and 24 deletions

View file

@ -7,10 +7,10 @@ public class Person
public int Age { get; set; } public int Age { get; set; }
// Constructor // Constructor
public Person(string firstName, string lastName, int Age) public Person(string firstName, string lastName, int age)
{ {
firstName = firstName; FirstName = firstName;
lastName = lastName; LastName = lastName;
Age = Age; Age = age;
} }
} }

View file

@ -2,28 +2,15 @@
{ {
class Program class Program
{ {
static public void AddPeople() static void Main(string[] args)
{ {
List<Person> people = new List<Person>(); List<Person> people = new List<Person>
Person person1 = new Person("Alice", "Smith", 25);
Person person2 = new Person("Bob", "Jones", 37);
Person person3 = new Person("Charlie", "Jones", 28);
people.Add(person1);
people.Add(person2);
people.Add(person3);
WriteToConsole(people);
}
static void WriteToConsole(this List<Person> people)
{
for (int i = 0; i < people.Count; i++)
{ {
Console.WriteLine($"Fornavn: {people[i].FirstName}\nEtternavn: {people[i].LastName}\nAlder: {people[i].Age}"); new Person("Alice", "Smith", 25),
} new Person("Bob", "Jones", 37),
} new Person("Charlie", "Jones", 28)
static void Main(string[] args){ };
AddPeople(); people.ForEach(p => Console.WriteLine($"Navn: {p.FirstName} {p.LastName}\nAlder: {p.Age}"));
} }
} }
} }