static void Main(string[] args) { List <Product> cart = new List <Product>(); Milk milk = new Milk(); milk.fatContent = 1; milk.ProductNumber = 123; milk.stockStatus = 10; cart.Add(milk); Nocco nocco = new Nocco(); nocco.flavor = "Cola"; nocco.ProductNumber = 234; nocco.stockStatus = 14; cart.Add(nocco); Coffe coffe = new Coffe(); coffe.roastyness = "Medium roast"; coffe.ProductNumber = 321; coffe.stockStatus = 0; cart.Add(coffe); foreach (Product product in cart) { if (product.GetType() == typeof(Milk)) { Console.WriteLine(((Milk)product).fatContent); } else if (product.GetType() == typeof(Coffe)) { Console.WriteLine(((Coffe)product).roastyness); } else if (product.GetType() == typeof(Nocco)) { Console.WriteLine(((Nocco)product).flavor); } } }
static void Main(string[] args) { List <Product> cart = new List <Product>(); Milk milk = new Milk(); milk.FatContent = 1; milk.ProductNumber = 123; milk.StockStatus = 10; Nocco nocco = new Nocco(); nocco.Flavor = "Cola"; nocco.ProductNumber = 234; nocco.StockStatus = 14; Coffee coffee = new Coffee(); coffee.Rostyness = "Dark AF"; coffee.ProductNumber = 345; coffee.StockStatus = 0; cart.Add(coffee); cart.Add(milk); cart.Add(nocco); foreach (var product in cart) { if (product.GetType() == typeof(Milk)) { Console.WriteLine(((Milk)product).GetType().Name); } else if (product.GetType() == typeof(Coffee)) { Console.WriteLine(((Coffee)product)); } else if (product.GetType() == typeof(Nocco)) { Console.WriteLine(((Nocco)product)); } } }