1. 主页
  2. 文档
  3. C#进阶
  4. 第五章 委托
  5. 第三节 委托的应用2

第三节 委托的应用2

建一个排序的类SimpleSortFirst.cs,并在里面写BubbleSort(int[] items)方法进行排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpAdvancedDelegate
{
    static class SimpleSortFirst
    {
        public static void BubbleSort(int[] items)
        {
            int i;
            int j;
            int temp;
            if (items==null)
            {
                return; 
            }
            for (i = items.Length-1; i >= 0; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    if (items[j-1]>items[j])
                    {
                        temp = items[j - 1];
                        items[j - 1] = items[j];
                        items[j] = temp;
                    }
                }
            }
        }
    }
}

这种普通方法实现的功能可以在Main函数中可以直接使用。

static void Main(string[] args)
{
       Console.WriteLine("=================普通排序=================");     
       int[] ietms = new int[] { 22, 8, 23, 34, 12, 3, 19 };
       SimpleSortFirst.BubbleSort(ietms);
       for (int i = 0; i < ietms.Length; i++)
       {
             Console.WriteLine(ietms[i] + " ");
       }
}

建一个排序的类SimpleSortSecond.cs,并在里面写BubbleSort(int[] items, SortType sortType)方法进行排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpAdvancedDelegate
{
    public enum SortType
    {
        Ascending,
        Descending
    }
    public static class SimpleSortSecond
    {
        public static void BubbleSort(int[] items, SortType sortType)
        {
            int i;
            int j;
            int temp;
            if (items == null)
            {
                return;
            }
            for (i = items.Length - 1; i >= 0; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    bool swap = false;
                    switch(sortType)
                    {
                        case SortType.Ascending:
                            swap = items[j - 1] > items[j];
                            break;
                        case SortType.Descending:
                            swap = items[j - 1] < items[j];
                            break;
                        default:
                            break;
                    }
                    if (swap)
                    {
                        temp = items[j - 1];
                        items[j - 1] = items[j];
                        items[j] = temp;
                    }
                }
            }
        }
    }
}

再新建一个委托排序的类DelegateSort.cs,在里面进行委托的定义

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpAdvancedDelegate
{
    delegate bool SortThan(int F, int S);  //委托定义
    class DelegateSort
    {
        public static void BubbleSort(int[] items,SortThan sortThan)
        {
            int i;
            int j;
            int temp;
            if (items == null)
            {
                return;
            }
            for (i = items.Length - 1; i >= 0; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    //if (items[j - 1] > items[j])
                    if (sortThan(items[j - 1] , items[j]))  //调用委托实例
                    {
                        temp = items[j - 1];
                        items[j - 1] = items[j];
                        items[j] = temp;
                    }
                }
            }
        }        
    }
}

在Program.cs入口类中添加委托函数GreateThan(int first, int second);GreateThan2(int first, int second)

 /// <summary>
 /// 委托要用的函数升序
 /// </summary>
 /// <param name="first"></param>
 /// <param name="second"></param>
 /// <returns></returns>
 public static bool GreateThan(int first, int second)
 {
       return first > second;
  }
 /// <summary>
 ///  委托要用的函数降序
 /// </summary>
 /// <param name="first"></param>
 /// <param name="second"></param>
 /// <returns></returns>
 public static bool GreateThan2(int first, int second)
 {
        return first < second;
 }

在Main()入口函数中添加代码

static void Main(string[] args)
{
      Console.WriteLine("=================委托排序=================");
      int[] ietms = new int[] { 22, 8, 23, 34, 12, 3, 19 };
      SortThan sortThan = new SortThan(GreateThan2); //实例化委托并关联

      DelegateSort.BubbleSort(ietms, sortThan);
      for (int i = 0; i < ietms.Length; i++)
      {
            Console.WriteLine(ietms[i] + " ");
      }
      Console.Read();
}
这篇文章对您有用吗? 1

我们要如何帮助您?