If the source collection is of a type that is not immutable, elements accessed through a ReadOnlyCollection
can be modified.
public class Item
{
public string Name { get; set; }
public decimal Price { get; set; }
}
public static void FillOrder()
{
// An order is generated
var order = new List<Item>
{
new Item { Name = "Apple", Price = 0.50m },
new Item { Name = "Banana", Price = 0.75m },
new Item { Name = "Vitamins", Price = 5.50m }
};
// The current sub total is $6.75
var subTotal = order.Sum(item => item.Price);
// Let the customer preview their order
var customerPreview = new ReadOnlyCollection<Item>(order);
// The customer can't add or remove items, but they can change
// the price of an item, even though it is a ReadOnlyCollection
customerPreview.Last().Price = 0.25m;
// The sub total is now only $1.50!
subTotal = order.Sum(item => item.Price);
}