using System;
using System.Collections.Generic;
namespace Example
{
class Program
{
static void Main(string[] args)
{
var list = new List<A>();
list.Add(new B("Anton", 25));
list.Add(new C("Anton", 0, 545));
list.Add(new D("Anton", 25, 545, 545474));
list.Add(new B("Anhjyton", 25));
list.Add(new C("Anton", 25, 545));
list.Add(new C("Antofn", 25, 1111));
foreach (A item in list)
{
D d = item as D;
if (d != null)
{
Console.WriteLine($"{d.Name} {d.Age} {d.Money} {d.Ssn}");
continue;
}
C c = item as C;
if (c != null)
{
Console.WriteLine($"{c.Name} {c.Age} {c.Money}");
continue;
}
B b = item as B;
if (b != null)
{
Console.WriteLine($"{b.Name} {b.Age}");
continue;
}
Console.WriteLine($"{item.Name}");
}
}
}
abstract class A
{
public string Name { get; set; }
protected A(string name)
{
Name = name;
}
}
class B : A
{
public int Age { get; set; }
public B(string name, int age)
: base(name)
{
Age = age;
}
}
class C : B
{
public int Money { get; set; }
public C(string name, int age, int money)
: base(name, age)
{
Money = money;
}
}
sealed class D : C
{
public int Ssn { get; set; }
public D(string name, int age, int money, int ssn)
: base(name, age, money)
{
Ssn = ssn;
}
}
}