Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace week7GenericsLab
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. MadLabClass<int> madClass = new MadLabClass<int>(10);
  14.  
  15. MadLabClass<int> anotherClass = new MadLabClass<int>(20);
  16.  
  17. madClass.myType[0] = 1;
  18. madClass.myType[1] = 2;
  19. madClass.myType[2] = 3;
  20. madClass.myType[3] = 4;
  21. madClass.myType[4] = 5;
  22.  
  23. madClass.DisplayValue();
  24.  
  25. Console.ReadKey();
  26. }
  27. }
  28.  
  29. class MadLabClass<TOne>
  30. {
  31. public int Index;
  32. public TOne[] myType;
  33.  
  34. public MadLabClass(int arraySize)
  35. {
  36. Index = 0;
  37. myType = new TOne[arraySize];
  38. }
  39.  
  40. public void DisplayValue()
  41. {
  42. Console.WriteLine("Question 1:");
  43. for (int i = 0; i <= myType.Length; i++)
  44. {
  45. if (i == myType.Length)
  46. {
  47. Console.WriteLine("Question 2\n");
  48. Console.ReadKey();
  49. break;
  50. }
  51. Console.WriteLine("Value is " + myType[i] + " at index " + i);
  52. }
  53.  
  54. int numX = 1;
  55. int numY = 2;
  56. string testStringA = "Hello World";
  57. string testStringB = "Good afternoon world";
  58.  
  59. talkToMe<string, int>(testStringA, numX, numY);
  60.  
  61. numX = 2;
  62. testStringA = "ASDF";
  63. talkToMe<int, string>(numX, testStringA, testStringB);
  64.  
  65. numX = 3;
  66. testStringA = "test string three";
  67. numY = 5;
  68. talkToMe<string, int>(testStringA, numX, numY);
  69.  
  70. Console.ReadKey();
  71.  
  72. }
  73.  
  74. public static void talkToMe<T1, T2>(T1 A, T2 B, T2 C)
  75. {
  76.  
  77. T1 objA = A;
  78.  
  79. T2 objB = B;
  80.  
  81. T2 objC = C;
  82.  
  83.  
  84. Console.WriteLine("ObjA holds: " + objA.ToString() + " - Data type: " + objA.GetType());
  85.  
  86. Console.WriteLine("ObjB holds: " + objB.ToString() + " - Data type: " + objB.GetType());
  87.  
  88. Console.WriteLine("ObjC holds: " + objC.ToString() + " - Datatype: " + objC.GetType());
  89.  
  90. }
  91.  
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement