Jak sprawić aby metoda "ShowListOfXs" mogła przyjmować listy klas głównej i pochodnych?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSHARP_EXP
{
interface IXable
{
int x { get; set; }
}
class Glowna : IXable
{
public int x { get; set; }
public Glowna(int x)
{
this.x = x;
}
}
class Pochodna : Glowna
{
public int y { get; set; }
public Pochodna(int x,int y) : base(x)
{
this.y = y;
}
}
class Program
{
private static void ShowListOfXs(List<IXable> lst)
{
foreach(var item in lst)
{
Console.WriteLine(item.x);
}
}
static void Main(string[] args)
{
List<Glowna> lst = new List<Glowna>() { new Glowna(2), new Glowna(6) };
List<Pochodna> lst2 = new List<Pochodna>() { new Pochodna(55,12), new Pochodna(34,2) };
ShowListOfXs(lst);
ShowListOfXs(lst2);
Console.ReadKey();
}
}
}