using System; using System.Collections.ObjectModel; using System.Collections.Generic; using System.Text; using System.Runtime.Serialization; public class Employee : System.IComparable { private int _id; private string _name; private string _rating; private static SortOrder _order; public enum SortOrder { Ascending = 0, Descending = 1 } public Employee(int id, string name) : this(id, name, "Other") { } public Employee(int id, string name, string rating) { this._id = id; this._name = name; this._rating = rating; } public int Id { get { return this._id; } set { this._id = value; } } public string Name { get { return this._name; } set { this._name = value; } } public string Rating { get { return this._rating; } set { this._rating = value; } } public static SortOrder Order { get { return _order; } set { _order = value; } } public override bool Equals(Object obj) { bool retVal = false; if (obj != null) { Employee custObj = (Employee)obj; if ((custObj.Id == this.Id) && (custObj.Name.Equals(this.Name) && (custObj.Rating.Equals(this.Rating)))) retVal = true; } return retVal; } public override string ToString() { return this._id + ": " + this._name; } public int CompareTo(Object obj) { switch (_order) { case SortOrder.Ascending: return this.Name.CompareTo(((Employee)obj).Name); case SortOrder.Descending: return (((Employee)obj).Name).CompareTo(this.Name); default: return this.Name.CompareTo(((Employee)obj).Name); } } } public class CollectionTest { public static void Main() { List<Employee> collCustList = new List<Employee>(); collCustList.Add(new Employee(99, "H", "P")); collCustList.Add(new Employee(77, "B", "G")); collCustList.Add(new Employee(55, "B", "G")); collCustList.Add(new Employee(88, "B", "P")); collCustList.Add(new Employee(11, "L", "O")); foreach (Employee cust in collCustList) Console.Out.WriteLine(cust); Employee.Order = Employee.SortOrder.Ascending; collCustList.Sort(delegate(Employee cust1, Employee cust2) { return Comparer<Employee>.Default.Compare(cust1, cust2); }); foreach (Employee cust in collCustList) Console.Out.WriteLine(cust); Employee.Order = Employee.SortOrder.Descending; collCustList.Sort(delegate(Employee cust1, Employee cust2) { return Comparer<Employee>.Default.Compare(cust1, cust2); }); foreach (Employee cust in collCustList) Console.Out.WriteLine(cust); } }
Add Collection Items with IComparable interface implementation
using System; using System.Collections.ObjectModel; using System.Collections.Generic; using System.Text; using System.Runtime.Serialization; public class Customer : System.IComparable { private int _id; private string _name; private string _rating; private static SortOrder _order; public enum SortOrder { Ascending = 0, Descending = 1 } public Customer(int id, string name) : this(id, name, "Other") { } public Customer(int id, string name, string rating) { this._id = id; this._name = name; this._rating = rating; } public int Id { get { return this._id; } set { this._id = value; } } public string Name { get { return this._name; } set { this._name = value; } } public string Rating { get { return this._rating; } set { this._rating = value; } } public static SortOrder Order { get { return _order; } set { _order = value; } } public override bool Equals(Object obj) { bool retVal = false; if (obj != null) { Customer custObj = (Customer)obj; if ((custObj.Id == this.Id) && (custObj.Name.Equals(this.Name) && (custObj.Rating.Equals(this.Rating)))) retVal = true; } return retVal; } public override string ToString() { return this._id + ": " + this._name; } public int CompareTo(Object obj) { switch (_order) { case SortOrder.Ascending: return this.Name.CompareTo(((Customer)obj).Name); case SortOrder.Descending: return (((Customer)obj).Name).CompareTo(this.Name); default: return this.Name.CompareTo(((Customer)obj).Name); } } } public class CollectionTest { public static void Main() { Collection<Customer> custColl = new Collection<Customer>(); custColl.Add(new Customer(1, "S")); custColl.Add(new Customer(2, "K")); custColl.Add(new Customer(3, "T")); custColl.Insert(1, new Customer(4, "Inserted Employee")); custColl[3] = new Customer(9, "F"); foreach (Customer cust in custColl) Console.Out.WriteLine(cust); } }
Add user-defined object to generic Collection
using System; using System.Collections.ObjectModel; using System.Collections.Generic; using System.Text; class Team { private string name; public Team(string name) { this.name = name; } public override string ToString() { return name; } } class Program { static void Main(string[] args) { Collection<Team> teams = new Collection<Team>(); teams.Add(new Team("Ferrari")); teams.Add(new Team("Williams-BMW")); teams.Add(new Team("Bar-Honda")); teams.Add(new Team("McLaren-Mercedes")); foreach (Team t in teams) { Console.WriteLine(t); } } }
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 ); } }
'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."); } }