public class Product
{
private string name;
public int ProductID { get; set; }
public string Name
{
get { return ProductID + name; }
set { name = value; }
}
public string Description { get; set; }
public decimal Price { get; set; }
public string Category { set; get; }
}
public class ShoppingCart
{
public List<Product> Products { get; set; }
}
假如 Product 跟 ShoppingCart 是无法修改的类 这时应用扩展方法
public static class MyExtensionMethods
{
public static decimal TotalPrices(this IEnumerable<Product> productEnum)
{
decimal total = 0;
foreach (Product prod in productEnum)
{
total += prod.Price;
}
return total;
}
}