using System; class A { } class B : A { } // Here, V must inherit T. class Gen<T, V> where V : T { } class Test { public static void Main() { // This declaration is OK because B inherits A. Gen<A, B> x = new Gen<A, B>(); // This declaration is in error because // A does not inherit B. // Gen<B, A> y = new Gen<B, A>(); } }
Author: coder
Generic Fields
using System; using System.Collections.Generic; using System.Text; class MyType<T, U> { private T _myFirstDataMember; private U _mySecondDataMember; public MyType(T val1, U val2) { this._myFirstDataMember = val1; this._mySecondDataMember = val2; } public T GetFirstDataMember() { return this._myFirstDataMember; } public U GetSecondDataMember() { return this._mySecondDataMember; } } class MyApp { static void main(String[] args) { MyType<string, string> testType = new MyType<string, string>("Val1", "Val2"); Console.WriteLine(testType.GetFirstDataMember()); Console.WriteLine(testType.GetSecondDataMember()); } }
Declare the generic class.
public class GenericList<T> { void Add(T input) { } } class TestGenericList { private class ExampleClass { } static void Main() { // Declare a list of type int. GenericList<int> list1 = new GenericList<int>(); // Declare a list of type string. GenericList<string> list2 = new GenericList<string>(); // Declare a list of type ExampleClass. GenericList<ExampleClass> list3 = new GenericList<ExampleClass>(); } }
The overriding method cannot change the inherited constraints.
using System; public class MyClass { public virtual void MethodA<T>(T arg) where T : new() { } } public class YClass : MyClass { public override void MethodA<T>(T arg) { T obj = new T(); } }
Overriding a virtual method in a generic class
using System; class MyBaseGenericClass<T> { protected T myObject; public MyBaseGenericClass(T o) { myObject = o; } public virtual T getmyObject() { Console.Write("Base's getmyObject(): " ); return myObject; } } class MyGenericClass<T> : MyBaseGenericClass<T> { public MyGenericClass(T o) : base(o) { } public override T getmyObject() { Console.Write("overide getmyObject(): "); return myObject; } } class Test { public static void Main() { MyBaseGenericClass<int> myObject = new MyBaseGenericClass<int>(8); Console.WriteLine(myObject.getmyObject()); myObject = new MyGenericClass<int>(9); Console.WriteLine(myObject.getmyObject()); } }
A non-generic class can be the base class of a generic derived class
using System; class MyBaseClass { int num; public MyBaseClass(int i) { num = i; } public int getnum() { return num; } } class MyGenericClass<T> : MyBaseClass { T ob; public MyGenericClass(T o, int i) : base (i) { ob = o; } public T getob() { return ob; } } class HierDemo3 { public static void Main() { MyGenericClass<String> w = new MyGenericClass<String>("Hello", 7); Console.WriteLine(w.getob()); Console.WriteLine(w.getnum()); } }
A derived class with its own type parameters
using System; class MyClass<T> { T ob; public MyClass(T o) { ob = o; } public T getob() { return ob; } } class ClassA<T, V> : MyClass<T> { V ob2; public ClassA(T o, V o2) : base(o) { ob2 = o2; } public V getob2() { return ob2; } } class Test { public static void Main() { ClassA<string, int> x = new ClassA<string, int>("Value is: ", 9); Console.Write(x.getob()); Console.WriteLine(x.getob2()); } }