时间:2023-01-01 12:44:17 | 栏目:.NET代码 | 点击:次
C#实现委托
namespace Delegate { delegate void DGSayiHi(string name);//声明委托 delegate void DGDo(string name); class Program { static void Main(string[] args) { DGSayiHi sayhi = new DGSayiHi(SayChineseHi); DGSayiHi sayhi2 = new DGSayiHi(SayEnglishHi); sayhi += sayhi2;//实际上+=相当于Delegate.Combine, -=相当于Delegate.Remove sayhi("春晓"); Console.ReadLine(); } static void SayChineseHi(string name) { Console.WriteLine("你好:" + name); } static void SayEnglishHi(string name) { Console.WriteLine("Hello:" + name); } } }
C#实现的从小到大的冒泡排序:
public void BubbleSort(int[] array) { int length = array.Length; for (int i = 0; i < length - 1; i++) { for (int j = length - 1; j > i; j--) { // 对两个元素进行交换 if (array[j] < array[j - 1]) { int temp = array[j]; array[j] = array[j - 1]; array[j - 1] = temp; } } } }