using System;
using System.Collections;
public class MyClass<T> where T : IComparable {
}
public class YClass<T> : MyClass<T> where T : IComparable {
}
public class XClass<T> : MyClass<T> where T : IComparable, IDisposable {
}
public class BClass<Y> where Y : IEnumerable {
}
public class AClass<Z> : BClass<int[]> where Z : IDisposable {
}
Generics
Nested Types
using System;
public class Starter {
public static void Main() {
MyClass<int>.Nested<double> obj =
new MyClass<int>.Nested<double>();
obj.MethodA(10, 12.34);
}
}
public class MyClass<T> {
public void MethodA(T arg) {
}
public class Nested<S> {
public void MethodA(T arg1, S arg2) {
Console.WriteLine("arg1: {0}",
arg1.GetType().ToString());
Console.WriteLine("arg2: {0}",
arg2.GetType().ToString());
}
}
}
Call ToString on generic type
using System;
using System.Collections.Generic;
using System.Text;
public class HelloGenerics<T> {
private T _thisTalker;
public T Talker {
get { return this._thisTalker; }
set { this._thisTalker = value; }
}
public void SayHello() {
string helloWorld = _thisTalker.ToString();
Console.WriteLine(helloWorld);
}
}
public class GermanSpeaker {
public override string ToString() {
return "GermanSpeaker!";
}
}
public class SpainishSpeaker {
public override string ToString() {
return "SpainishSpeaker";
}
}
public class EnglishSpeaker {
public override string ToString() {
return "EnglishSpeaker";
}
}
class Program {
static void Main(string[] args) {
HelloGenerics<GermanSpeaker> talker1 = new HelloGenerics<GermanSpeaker>();
talker1.Talker = new GermanSpeaker();
talker1.SayHello();
HelloGenerics<SpainishSpeaker> talker2 = new HelloGenerics<SpainishSpeaker>();
talker2.Talker = new SpainishSpeaker();
talker2.SayHello();
}
}
Inherit Type Parameter
using System;
using System.Collections.Generic;
using System.Text;
public class MyBaseClass<U> {
private U _parentData;
public MyBaseClass() { }
public MyBaseClass(U val) {
this._parentData = val;
}
}
public class MySubClass<T, U> : MyBaseClass<U> {
private T _myData;
public MySubClass() { }
public MySubClass(T val1, U val2)
: base(val2) {
this._myData = val1;
}
}
Output the type information about the generic parameters
using System;
using System.Collections.Generic;
using System.Text;
public class OneParamType<T> {}
public class TwoParamType<T, U> {}
public class TypeDumper<T, U, V> {
public static void DumpTypeInfo() {
Console.WriteLine(typeof(T));
Console.WriteLine(typeof(U));
Console.WriteLine(typeof(V));
Console.WriteLine(typeof(OneParamType<String>));
Console.WriteLine(typeof(OneParamType<T>));
Console.WriteLine(typeof(TwoParamType<U, int>));
Console.WriteLine(typeof(TwoParamType<T, V>));
}
public static void ShowTypeInfo() {
TypeDumper<String, int, Double>.DumpTypeInfo();
}
}
A generic class with two generic parameters
using System;
using System.Collections.Generic;
using System.Text;
class MyCache<K, V> {
private static Dictionary<K, V> _objectCache;
public MyCache() {
MyCache<K, V>._objectCache = new Dictionary<K, V>();
}
private V FindValueInDB(K key) {
return default(V);
}
public V LookupValue(K key) {
V retVal;
if (_objectCache.ContainsKey(key) == true) {
_objectCache.TryGetValue(key, out retVal);
} else {
retVal = FindValueInDB(key);
}
return retVal;
}
}
class MyApp {
public static void main(String[] args) {
MyCache<string, string> cache1 = new MyCache<string, string>();
string val1 = cache1.LookupValue("key1");
MyCache<string, int> cache2 = new MyCache<string, int>();
int val2 = cache2.LookupValue("key1");
}
}
Walk through a tree recursively
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Tree<TItem> where TItem : IComparable<TItem> {
public Tree(TItem nodeValue) {
this.NodeData = nodeValue;
this.LeftTree = null;
this.RightTree = null;
}
public void Insert(TItem newItem) {
TItem currentNodeValue = this.NodeData;
if (currentNodeValue.CompareTo(newItem) > 0) {
if (this.LeftTree == null) {
this.LeftTree = new Tree<TItem>(newItem);
} else {
this.LeftTree.Insert(newItem);
}
} else {
if (this.RightTree == null) {
this.RightTree = new Tree<TItem>(newItem);
} else {
this.RightTree.Insert(newItem);
}
}
}
public void WalkTree() {
if (this.LeftTree != null) {
this.LeftTree.WalkTree();
}
Console.WriteLine(this.NodeData.ToString());
if (this.RightTree != null) {
this.RightTree.WalkTree();
}
}
public TItem NodeData { get; set; }
public Tree<TItem> LeftTree { get; set; }
public Tree<TItem> RightTree { get; set; }
}
class Program {
static void Main(string[] args) {
Tree<int> tree1 = new Tree<int>(10);
tree1.Insert(5);
tree1.Insert(1);
tree1.Insert(15);
tree1.Insert(-2);
tree1.Insert(115);
tree1.Insert(10);
tree1.Insert(114);
tree1.Insert(-18);
tree1.Insert(1110);
tree1.Insert(81);
tree1.Insert(18);
tree1.WalkTree();
Tree<string> tree2 = new Tree<string>("Hello");
tree2.Insert("A");
tree2.Insert("B");
tree2.Insert("C");
tree2.Insert("D");
tree2.Insert("E");
tree2.Insert("F");
tree2.Insert("G");
tree2.Insert("H");
tree2.Insert("I");
tree2.Insert("J");
tree2.Insert("K");
tree2.WalkTree();
}
}