Just for fun, let's constrain InventoryList to only be able to accept types that are classes:
- Open up InventoryList and add in the following code:
// 1
public class InventoryList<T> where T: class
{
// ... No changes needed ...
}
Since our inventoryList instance, the example uses strings, and strings are classes, so there's no problem with our code. However, if we were to change the type constraint to a struct or an interface name, our generic class would throw an error. This kind of situation is especially useful when you need to safeguard your generic classes or methods against types that you don't want to support.