Hi,
I'm noob in C# and I tried to display elements from List<T> using the following way:
It's possible to see element from List using this way ? (derivate class based inheritance).
Right now after I run this code the List is empty, of course if I move method Show() inside Student class all is good but i don't want.to use this way.
Thank you!
I'm noob in C# and I tried to display elements from List<T> using the following way:
It's possible to see element from List using this way ? (derivate class based inheritance).
Right now after I run this code the List is empty, of course if I move method Show() inside Student class all is good but i don't want.to use this way.
Thank you!
C#:
class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
class Student
{
public List<Person> li = new List<Person>();
public void Add(Person p)
{
li.Add(p);
}
}
class Tools : Student
{
public void Show()
{
foreach (Person p in li)
{
Console.WriteLine($"Display: [Name {p.Name[0]} and age {p.Age}");
}
}
}
static void Main(string[] args)
{
Person p1 = new Person () { Id = 1, Name = "Xxxxx", Age = 33 };
Person p2 = new Person () { Id = 2, Name = "Yyyyy", Age = 66 };
Person p3 = new Person () { Id = 3, Name = "Zzzzz", Age = 99 };
Student e3 = new Student();
e3.Add(p1);
e3.Add(p2);
e3.Add(p3);
Tools t= new Tools();
t.Show();
}