Generic Comparer RRS feed

  • Question

  • User-1319896600 posted
            public class MyGenericComparer<T> 
                public int Compare(T x, T y)
                    if (typeof(T) == typeof(String))
                        return ((string)(object)x).CompareTo((string)(object)y); 
                    if (typeof(T) == typeof(int))
                        return ((int)(object)x).CompareTo((int)(object)y); 
    //type unknown
    { return -999;
    } } }

    Hi, I am trying to implement a generic comparer, as you can see, this is probably the most raw / bad form of an implementation. I was trying to see if I can look at how

    Comparer<T> was implemented which derives from IComparer and IComparer<T>. But the code is not visible. Perhaps using reflection, might be possible. But I want to understand the idea behind a good comparer solution. Any help is much appreciated


    Tuesday, March 24, 2015 6:52 AM


All replies