Generic Collection and List

   
  
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);
    }
}

   
     


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 );
    }
}


           
         
     


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.");
  }
}
           
         
     


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());
    }
}