Select Page

If you ever find yourself wanting to duplicate code with just slight changes to adapt it to use a different type, then you’ll appreciate C# generics. Generic programming is sometimes called template programming because it allows you to write code that will be used later to generate the actual specific code.

Lets say you have a class called Shop and another class called InventoryItem like this:

You can trade your inventory at the shop and this works well until you need to trade things that may not actually be inventory. So you create an interface called ITradable that any class can implement if it wants to be traded.

Now you can trade items as well as service. And then you need to make sure that only some items can be traded at certain stores. You could add a new property to the interface to get a category either as a string or some number representation but this approach still has problems as the podcast episode describes. Instead, you make the shop a generic class like this: