如何使用Compareto()方法 [英] How Compareto() Method used

查看:157
本文介绍了如何使用Compareto()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,但没有得到它的概念.我在其中使用了泛型.

I have made one program and but not getting its concept.I have used Generics in this.

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用于排序
这里t1是一个对象,t2是一个实例
我的问题是,它在什么基础上进行排序
在这里,
如果(t2.CompareTo(t1)> 0)
这是什么意思

一样 if(t2.CompareTo(t1)< 0)

如果(t2.CompareTo(t1)== 0)
请告诉我这个概念
预先感谢



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

推荐答案

由于很难阅读MSDN帮助页面http://msdn.microsoft.com/en-us/library/system.icomparable.compareto.aspx [
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


检查这些链接

http://msdn.microsoft.com/en-us/library/system.icomparable. compareto.aspx [ ^ ]

http://msdn.microsoft.com/zh-我们/library/system.icomparable.compareto%28v=vs.71%29.aspx [
Check these links

http://msdn.microsoft.com/en-us/library/system.icomparable.compareto.aspx[^]

http://msdn.microsoft.com/en-us/library/system.icomparable.compareto%28v=vs.71%29.aspx[^]


这篇关于如何使用Compareto()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆