[csharp] Generics
Viewer
- // See https://aka.ms/new-console-template for more information
- namespace Generics0
- {
- class Program
- {
- static void Main(string[] args)
- {
- ManagesAnything<Item> manageItems = new ManagesAnything<Item>();
- Lumber lumber = new Lumber();
- Gold gold = new Gold();
- for (int i = 0; i < 3; i++)
- {
- manageItems.AddAmount(100, lumber);
- manageItems.AddAmount(100, gold);
- Console.WriteLine("LUMBER:: " + lumber.Current);
- Console.WriteLine("GOLD:: " + gold.Current);
- }
- }
- }
- class ManagesLumber
- {
- public void AddAmount(int addition, Item item)
- {
- item.Current += addition;
- }
- }
- class ManagesAnything<T>
- {
- public void AddAmount(int addition, Item item)
- {
- item.Current += addition;
- }
- }
- class Item
- {
- public int Current { get; set; }
- public int Limit { get; set; }
- }
- class Lumber : Item
- {
- public Lumber()
- {
- Current = 100;
- }
- }
- class Gold : Item
- {
- public Gold()
- {
- Current = 1000;
- }
- }
- }
Editor
You can edit this paste and save as new: