Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have made one program and but not getting its concept.I have used Generics in this.

C#
class Program
    {
        class CompGen<T>
where T : IComparable
        {
            public T t1;
            public T t2;
            public CompGen(T _t1, T _t2)
            {
                t1 = _t1;
                t2 = _t2;
            }
            public T Max()
            {
                if (t2.CompareTo(t1)> 0)
                    return t1;
                else
                    return t2;
            }
        }
        static void Main(string[] args)
        {
            CompGen<string> ga = new CompGen<string>("hello ", "World!");
            Console.WriteLine(ga.Max());
            Console.ReadLine();
        }
    }


Icomaparable is used for sorting
here t1 is an object and t2 is an instance
My question is that on what basis it is sorting
Here it is,
if (t2.CompareTo(t1)> 0)
what does it mean
same for
if (t2.CompareTo(t1)< 0)
and
if (t2.CompareTo(t1)== 0)
Please tell me this concept
Thanx in advance
Posted

Was it so difficult to read the MSDN help page http://msdn.microsoft.com/en-us/library/system.icomparable.compareto.aspx[^]?

First comparison it true means that t1 < t2 (instance follows parameter); last one means the instance and parameters are at the same position, t1 == t2, the second one… well, you should get the picture by now…

—SA
 
Share this answer
 
Comments
koolprasad2003 9-Nov-11 1:58am    
nice ans SA, 5.
Sergey Alexandrovich Kryukov 9-Nov-11 1:59am    
Thank you, Prasad.
--SA
shivani 2013 9-Nov-11 5:31am    
Thank you sir
Sergey Alexandrovich Kryukov 9-Nov-11 10:13am    
You are welcome.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900