using System; using System.Collections.Generic; public class Book : IComparable<Book> { private string name; private int circulation; private class AscendingCirculationComparer : IComparer<Book> { public int Compare(Book x, Book y) { if (x == null && y == null) return 0; else if (x == null) return -1; else if (y == null) return 1; if (x == y) return 0; return x.circulation - y.circulation; } } public Book(string name, int circulation) { this.name = name; this.circulation = circulation; } public static IComparer<Book> CirculationSorter { get { return new AscendingCirculationComparer(); } } public override string ToString() { return string.Format("{0}: Circulation = {1}", name, circulation); } public int CompareTo(Book other) { if (other == null) return 1; if (other == this) return 0; return string.Compare(this.name, other.name, true); } } public class MainClass { public static void Main() { List<Book> Books = new List<Book>(); Books.Add(new Book("E", 1)); Books.Add(new Book("T", 5)); Books.Add(new Book("G", 2)); Books.Add(new Book("S", 8)); Books.Add(new Book("H", 5)); foreach (Book n in Books) { Console.WriteLine(" " + n); } Books.Sort(); foreach (Book n in Books) { Console.WriteLine(" " + n); } Books.Sort(Book.CirculationSorter); foreach (Book n in Books) { Console.WriteLine(" " + n); } } }
Implements IComparable
Reference for Generic Types
using System; public class Starter { public static void Main() { MyClass<int> obj = new MyClass<int>(); obj.MethodA(); } } class MyClass<T> { public T MethodA() { T var = default(T); Console.WriteLine(this.GetType().ToString()); return var; } }
A generic Point structure.
using System; using System.Collections.Generic; using System.Text; public struct Point<T> { private T xPos; private T yPos; public Point(T xVal, T yVal) { xPos = xVal; yPos = yVal; } public T X { get { return xPos; } set { xPos = value; } } public T Y { get { return yPos; } set { yPos = value; } } public override string ToString() { return string.Format("[{0}, {1}]", xPos, yPos); } public void ResetPoint() { xPos = default(T); yPos = default(T); } }
combining inheritance of generic types and constraints:
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 { }
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; } }