当前位置: 首页>>代码示例>>C#>>正文


C# Car.CompareTo方法代码示例

本文整理汇总了C#中Car.CompareTo方法的典型用法代码示例。如果您正苦于以下问题:C# Car.CompareTo方法的具体用法?C# Car.CompareTo怎么用?C# Car.CompareTo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Car的用法示例。


在下文中一共展示了Car.CompareTo方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: implementsIComparable

        private void implementsIComparable()
        {
            Console.WriteLine("Implement IComparable Interface...");
            Console.WriteLine("Create 2 objects of type 'car' then compare car1 to car2\nIt is possible to use Array.Sort to sort an array of Car objects because the 'Car' class implements the IComparable interface");
            Car car1 = new Car();
            car1.Name = "Volvo 850";
            car1.Price = 5000;
            car1.MaxMph = 85;
            car1.Horsepower = 1200;

            Car car2 = new Car();
            car2.Name = "Mazda Miata MX5";
            car2.Price = 12000;
            car2.MaxMph = 120;
            car2.Horsepower = 1100;

            Console.WriteLine("\nCar 1 = " + car1.Name + ", value = " + car1.Price + ", horsepower = " + car1.Horsepower + ", MaxMph = " + car1.MaxMph + "\nCar 2 = " + car2.Name + ", value = " + car2.Price + ", horsepower = " + car2.Horsepower + ", MaxMph = " + car2.MaxMph);

            Console.WriteLine("car1.compareTo(car2) returns the value " + car1.CompareTo(car2).ToString());
            Console.WriteLine("car2.CompareTo(car1) returns the value " + car2.CompareTo(car1).ToString());

            //because the Car type implements the IComparable interface, we can use Array.Sort to sort an array of car objects
            Car[] cars = new Car[2];
            cars[0] = car1;
            cars[1] = car2;

            Console.WriteLine("\nBefore sorting the array...");
            for (int i = 0; i < cars.Length; i++)
            {
                Console.WriteLine("Cars[" + i + "] = " + cars[i].Name);
            }

            Array.Sort(cars);
            Console.WriteLine("\nAfter sorting the array...");
            for (int i = 0; i < cars.Length; i++)
            {
                Console.WriteLine("Cars[" + i + "] = " + cars[i].Name);
            }

            Program.dividingLine();

            try
            {
                bool firstRun = true;
                string yesNo = "";
                do
                {

                    if (!firstRun)
                    {
                        Console.WriteLine("\nSelect another property to compare by? y|n");
                        yesNo = Console.ReadLine().ToLower();
                    }

                    if (yesNo != "n")
                    {
                        compareByChoice(cars);
                    }

                    firstRun = false;

                } while (yesNo != "n");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
开发者ID:CronanGogarty,项目名称:MCSD-Certification-Toolkit-70-483,代码行数:68,代码来源:Chapter5.cs


注:本文中的Car.CompareTo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。