Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Syntax
- {
- public class GenericClass<T>
- {
- private class Node
- {
- private T data;
- private Node next;
- public Node(T t)
- {
- next = null;
- data = t;
- }
- public Node Next { get { return next; } set { next = value; } }
- public T Data { get { return data; } set { data = value; } }
- }
- private Node head;
- public GenericClass()
- {
- head = null;
- }
- public void AddHead(T t)
- {
- Node n = new Node(t);
- n.Next = head;
- head = n;
- }
- public IEnumerator<T> GetEnumerator()
- {
- Node current = head;
- while (current != null)
- {
- yield return current.Data;
- current = current.Next;
- }
- }
- public static void Showcase()
- {
- GenericClass<int> list = new GenericClass<int>();
- for(int x = 0; x <10; x++)
- {
- list.AddHead(x);
- }
- System.Console.WriteLine("nPrinting generic class using ints.");
- foreach (int i in list)
- {
- System.Console.Write(i + ", ");
- }
- GenericClass<char> listChars = new GenericClass<char>();
- string abc = "abcdefghijklmnopqrstuvw";
- foreach (char c in abc)
- {
- listChars.AddHead(c);
- }
- System.Console.WriteLine("nnPrinting generic class using chars.");
- foreach (var c in listChars)
- {
- System.Console.Write(c + ", ");
- }
- }
- }
- }
- GenericClass<int>.Showcase();
- GenericClass<string>.Showcase();
- GenericClass<char>.Showcase();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement