[csharp] Generics

Viewer

copydownloadembedprintName: Generics
  1. // See https://aka.ms/new-console-template for more information
  2.  
  3. namespace Generics0
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             ManagesAnything<Item> manageItems = new ManagesAnything<Item>();
  10.  
  11.             Lumber lumber = new Lumber();
  12.             Gold gold = new Gold();
  13.  
  14.             for (int i = 0; i < 3; i++)
  15.             {
  16.                 manageItems.AddAmount(100, lumber);
  17.                 manageItems.AddAmount(100, gold);
  18.  
  19.                 Console.WriteLine("LUMBER:: " + lumber.Current);
  20.                 Console.WriteLine("GOLD:: " + gold.Current);
  21.  
  22.             }
  23.         }
  24.     }
  25.  
  26.     class ManagesLumber
  27.     {
  28.         public void AddAmount(int addition, Item item)
  29.         {
  30.             item.Current += addition;
  31.         }
  32.     }
  33.     class ManagesAnything<T>
  34.     {
  35.         public void AddAmount(int addition, Item item)
  36.         {
  37.             item.Current += addition;
  38.         }
  39.     }
  40.  
  41.     class Item
  42.     {
  43.         public int Current { get; set; }
  44.         public int Limit { get; set; }
  45.     }
  46.     class Lumber : Item
  47.     {
  48.         public Lumber()
  49.         {
  50.             Current = 100;
  51.         }
  52.     }
  53.     class Gold : Item
  54.     {
  55.         public Gold()
  56.         {
  57.             Current = 1000;
  58.         }
  59.     }
  60. }

Editor

You can edit this paste and save as new:


File Description
  • Generics
  • Paste Code
  • 30 Jun-2022
  • 1.28 Kb
You can Share it: