Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. namespace Syntax
  2. {
  3. public class GenericClass<T>
  4. {
  5. private class Node
  6. {
  7. private T data;
  8. private Node next;
  9.  
  10. public Node(T t)
  11. {
  12. next = null;
  13. data = t;
  14. }
  15.  
  16. public Node Next { get { return next; } set { next = value; } }
  17. public T Data { get { return data; } set { data = value; } }
  18. }
  19.  
  20.  
  21. private Node head;
  22. public GenericClass()
  23. {
  24. head = null;
  25. }
  26.  
  27. public void AddHead(T t)
  28. {
  29. Node n = new Node(t);
  30. n.Next = head;
  31. head = n;
  32. }
  33.  
  34. public IEnumerator<T> GetEnumerator()
  35. {
  36. Node current = head;
  37. while (current != null)
  38. {
  39. yield return current.Data;
  40. current = current.Next;
  41. }
  42. }
  43.  
  44.  
  45. public static void Showcase()
  46. {
  47. GenericClass<int> list = new GenericClass<int>();
  48.  
  49. for(int x = 0; x <10; x++)
  50. {
  51. list.AddHead(x);
  52. }
  53.  
  54. System.Console.WriteLine("nPrinting generic class using ints.");
  55. foreach (int i in list)
  56. {
  57. System.Console.Write(i + ", ");
  58. }
  59.  
  60. GenericClass<char> listChars = new GenericClass<char>();
  61. string abc = "abcdefghijklmnopqrstuvw";
  62. foreach (char c in abc)
  63. {
  64. listChars.AddHead(c);
  65. }
  66.  
  67. System.Console.WriteLine("nnPrinting generic class using chars.");
  68. foreach (var c in listChars)
  69. {
  70. System.Console.Write(c + ", ");
  71. }
  72. }
  73. }
  74. }
  75.  
  76. GenericClass<int>.Showcase();
  77.  
  78. GenericClass<string>.Showcase();
  79. GenericClass<char>.Showcase();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement