Pi0trek

Wojtek

Mar 20th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 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. class A
  8. {
  9.     public void hello()
  10.  
  11.     {
  12.         Console.WriteLine("hello");
  13.     }
  14. }
  15.  
  16. class B : A { }
  17.  
  18. class C { }  
  19.  
  20. class Test<T> where T : A
  21. {
  22.     T obj;
  23.     public Test(T o)
  24.     {
  25.         obj = o;
  26.     }
  27.  
  28.     public void sayHellow()
  29.     {
  30.         obj.hello();
  31.     }
  32. }
  33.  
  34. class BaseClassConstraintDemo
  35. {
  36.     public static void Main()
  37.     {
  38.         A a = new A();
  39.         B b = new B();
  40.         C c = new C();
  41.  
  42.         Test<A> t1 = new Test<A>(a);
  43.         t1.sayHellow();
  44.  
  45.         Test<B> t2 = new Test<B>(b);
  46.         t2.sayHellow();
  47.  
  48.         Test<C> t3 = new Test<C>(c);
  49.         t3.sayHellow();
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment