using System; using System.Collections.ObjectModel; using System.Collections.Generic; using System.Text; using System.Runtime.Serialization; public class CollectionTest { public static void Main() { Collection<int> intCollection = new Collection<int>(); List<String> strList = new List<String>(); strList.Add("Val1"); strList.Add("Val2"); Collection<String> strCollection = new Collection<String>(strList); foreach (String strVal1 in strCollection) System.Console.WriteLine(strVal1); strList[0] = "Val Changed"; foreach (String strVal2 in strCollection) System.Console.WriteLine(strVal2); } }
Generics
Generic class with interface
using System; using System.Collections.Generic; public interface IShape { double Area { get; } } public class Circle : IShape { public Circle( double radius ) { this.radius = radius; } public double Area { get { return 3.14 * radius * radius; } } private double radius; } public class Rect : IShape { public Rect( double width, double height ) { this.width = width; this.height = height; } public double Area { get { return width*height; } } private double width; private double height; } public class Shapes<T> where T: IShape { public double TotalArea { get { double acc = 0; foreach( T shape in shapes ) { acc += shape.Area; } return acc; } } public void Add( T shape ) { shapes.Add( shape ); } private List<T> shapes = new List<T>(); } public class Test { static void Main() { Shapes<IShape> shapes = new Shapes<IShape>(); shapes.Add( new Circle(3) ); shapes.Add( new Rect(7, 5) ); Console.WriteLine( "Total Area: {0}", shapes.TotalArea ); } }
'This' Reference for Generic Types
using System; public class Starter{ public static void Main(){ MyGenericClass<int> obj=new MyGenericClass<int>(); obj.MethodA(); } } class MyGenericClass<T> { public T MethodA() { T var=default(T); Console.WriteLine(this.GetType().ToString()); return var; } }
Comparing Instances of a Type Parameter
using System; class MyClass : IComparable { public int val; public MyClass(int x) { val = x; } public int CompareTo(object obj) { return val - ((MyClass) obj).val; } } class CompareDemo { public static bool contains<T>(T what, T[] obs) where T : IComparable { foreach(T v in obs) if(v.CompareTo(what) == 0) return true; return false; } public static void Main() { int[] nums = { 1, 2, 3, 4, 5 }; if(contains(2, nums)) Console.WriteLine("2 is found."); string[] strs = { "one", "two", "three"}; if(contains("two", strs)) Console.WriteLine("two is found."); MyClass[] mcs = { new MyClass(1), new MyClass(2), new MyClass(3), new MyClass(4) }; if(contains(new MyClass(3), mcs)) Console.WriteLine("MyClass(3) is found."); } }
Demonstrate the default keyword
using System; class MyClass { } class Test<T> { public T obj; public Test() { obj = default(T); } } class DefaultDemo { public static void Main() { Test<MyClass> x = new Test<MyClass>(); if(x.obj == null) Console.WriteLine("x.obj is null."); Test<int> y = new Test<int>(); if(y.obj == 0) Console.WriteLine("y.obj is 0."); } }
Create relationship between two type parameters
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>(); } }
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()); } }