fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Box<T> {
  5. private T t; // T stands for "Type"
  6. public void set(T t) {
  7. this.t = t;
  8. }
  9. public T get() {
  10. return t;
  11. }
  12. }
  13. public class BoxDemo {
  14.  
  15. public static void addBox<T>(T u, List<Box<T>> boxes) {
  16. Box<T> box = new Box<T>();
  17. box.set(u);
  18. boxes.Add(box);
  19. }
  20.  
  21. public static void outputBoxes<T>(List<Box<T>> boxes) {
  22. int counter = 0;
  23. foreach (Box<T> box in boxes) {
  24. T boxContents = box.get();
  25. Console.WriteLine("Box #" + counter + " contains [" +
  26. boxContents.ToString() + "]");
  27. counter++;
  28. }
  29. }
  30.  
  31. public static void Main(String[] args) {
  32. List<Box<int>> listOfIntegerBoxes =
  33. new List<Box<int>>();
  34. BoxDemo.addBox(10, listOfIntegerBoxes);
  35. BoxDemo.addBox(20, listOfIntegerBoxes);
  36. BoxDemo.addBox(30, listOfIntegerBoxes);
  37. BoxDemo.outputBoxes(listOfIntegerBoxes);
  38. }
  39. }
Success #stdin #stdout 0.03s 33920KB
stdin
Standard input is empty
stdout
Box #0 contains [10]
Box #1 contains [20]
Box #2 contains [30]