-
-
Save belzecue/58fad2afd648d73865544253271e019a to your computer and use it in GitHub Desktop.
An exmple of generics in c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
public interface IUseMagic | |
{ | |
void UseMagic(); | |
} | |
public class MagicUser : IUseMagic | |
{ | |
private string userName; | |
public MagicUser(string name){userName = name;} | |
public void UseMagic(){Console.WriteLine(userName + " used magic");} | |
} | |
public class Node<T> where T : IUseMagic | |
{ | |
private Node<T> next; | |
private T data; | |
public Node(T t) | |
{ | |
data = t; | |
} | |
public T GetData(){ return data; } | |
public Node<T> GetNext(){ return next; } | |
public void SetNext(Node<T> node){ next = node; } | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
Node<MagicUser> k = new Node<MagicUser>(new MagicUser("Jackie Chaaan")); | |
Node<MagicUser> k2 = new Node<MagicUser>(new MagicUser("Micheal Juksun")); | |
Node<MagicUser> k3 = new Node<MagicUser>(new MagicUser("Hawri Pouteur")); | |
Node<MagicUser> k4 = new Node<MagicUser>(new MagicUser("Fan Boi Chau")); | |
k.SetNext(k2); | |
k2.SetNext(k3); | |
k3.SetNext(k4); | |
Node<MagicUser> cur = k; | |
while(cur != null) | |
{ | |
cur.GetData().UseMagic(); | |
cur = cur.GetNext(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment