code_junkie

C# generics question

Nov 11th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. //Generic
  2. class Person<T> {
  3.  
  4. T var1;
  5.  
  6. public Person(T yer) {
  7. var1 = yer;
  8. }
  9.  
  10. public T Value { get { return var1; } }
  11. }
  12.  
  13. //Normal class
  14. class Human {
  15.  
  16. int var1;
  17.  
  18. public Human(int yer) {
  19. var1 = yer;
  20. }
  21.  
  22. public int Value { get { return var1; } }
  23. }
  24.  
  25. for (int i = 0; i < 1000000; i++) {
  26. Person<int> me = new Person<int>(1);
  27. int hey = me.Value;
  28. }
  29.  
  30. for (int i = 0; i < 1000000; i++) {
  31. Human per = new Human(1);
  32. object her = (object)per.Value;
  33. }
  34.  
  35. List<Human> myList = new List<Human>();
  36. Human h = new Human();
  37. myList.Add(h);
  38.  
  39. ArrayList myObjectList = new ArrayList();
  40. Human h = new Human();
  41. myObjectList.Add((object)h));
  42.  
  43. List<Person> pList = new List<Person>();
  44. for(int i = 0; i<1000; ++i)
  45. pList.Add(new Person(30));
  46.  
  47. StopWatch sw = new StopWatch();
  48. sw.start();
  49. int sum = 0;
  50. foreach(Person p in pList)
  51. sum += p.Value;
  52. sw.Stop();
  53.  
  54. ArrayList hList = new ArrayList;
  55. for(int i = 0; i<1000; ++i)
  56. hList.Add(new Human(30));
  57.  
  58. StopWatch sw = new StopWatch();
  59. sw.start();
  60. int sum = 0;
  61. foreach(Object h in hList)
  62. sum += ((Human)h).Value;
  63. sw.Stop();
  64.  
  65. class Human
  66. {
  67. object var1;
  68. public Human(int yer)
  69. {
  70. var1 = (object) yer;
  71. }
  72. public int Value
  73. {
  74. get { return (int) var1; }
  75. }
  76. }
  77.  
  78. object her = (object)per.Value;
  79.  
  80. int her = per.Value;
Add Comment
Please, Sign In to add comment