ASP.NET 泛型方法:两个数字进行交换位置

 public class Person
    {

      //当 lhs > rhs 的时候,就进行交换位置
        public void SwapIfGreater<T>( ref T lhs, ref T rhs ) where T : System.IComparable<T>
        {
            T temp;
            if ( lhs.CompareTo( rhs ) > 0 )
            {
                temp = lhs;
                lhs = rhs;
                rhs = temp;
            }
        }
    }

class Program
    {
        static void Main( string[ ] args )
        {
            Person p = new Person( );
            int a=5;
            int b=3;
            p.SwapIfGreater<int>( ref a, ref b );

            Console.WriteLine( "a= {0} b={1}",a,b);
        }
    }

 

posted @ 2012-07-30 17:27  blog_yuan  阅读(262)  评论(0编辑  收藏  举报