This question is as a result of trying Jon Skeet's answer to this question.
So I have the following code based in the question and answer from the above question link.
public abstract class DeliveryStrategy { }
public class ParcelDelivery : DeliveryStrategy { }
public class ShippingContainer : DeliveryStrategy { }
public abstract class Order<TDelivery> where TDelivery : DeliveryStrategy
{
private TDelivery delivery;
protected Order(TDelivery delivery)
{
this.delivery = delivery;
}
public TDelivery Delivery
{
get { return delivery; }
set { delivery = value; }
}
}
public class CustomerOrder : Order<ParcelDelivery>
{
public CustomerOrder()
: base(new ParcelDelivery())
{ }
}
public class OverseasOrder : Order<ShippingContainer>
{
public OverseasOrder() : base(new ShippingContainer())
{
}
}
I'm trying understand generics more to improve my skill set, so I have the question of: Now how can I use foreach to loop through a collection of "Orders"? I'm using C#2.0.
Code Example of what I'm trying to do (does not compile).
List<Order> orders = new List<Order>();
orders.Add(new CustomerOrder());
orders.Add(new CustomerOrder());
orders.Add(new OverseasOrder());
orders.Add(new OverseasOrder());
foreach (Order order in orders)
{
order.Delivery.ToString();
}
EDIT: Added OverseasOrder to give a better example.