C#代表

来源:互联网 发布:php默认编码方式 编辑:程序博客网 时间:2024/06/10 04:52
using System;class Program{    static void Main(string[] args) {        SortClass.Sort up = new SortClass.Sort(Ascending);        SortClass.Sort down = new SortClass.Sort(Descending);        SortClass doIT = new SortClass();        SortClass.val1 = 310;        SortClass.val2 = 220;        Console.WriteLine("Before Sort: val1 = {0}, val2 = {1}", SortClass.val1, SortClass.val2);        doIT.DoSort(up);        Console.WriteLine("After Sort: val1 = {0}, val2 = {1}", SortClass.val1, SortClass.val2);        Console.WriteLine("Before Sort: val1 = {0}, val2 = {1}", SortClass.val1, SortClass.val2);        doIT.DoSort(down);        Console.WriteLine("After Sort: val1 = {0}, val2 = {1}", SortClass.val1, SortClass.val2);    }    public static void Ascending(ref int first, ref int second) {        if (first > second) {            int tmp = first;            first = second;            second = tmp;        }    }    public static void Descending(ref int first, ref int second) {        if (first < second) {            int tmp = first;            first = second;            second = tmp;        }    }}public class SortClass{    static public int val1;    static public int val2;    public delegate void Sort(ref int a, ref int b);    public void DoSort(Sort ar) {        ar(ref val1, ref val2);    }}

Before Sort: val1 = 310, val2 = 220After Sort: val1 = 220, val2 = 310Before Sort: val1 = 220, val2 = 310After Sort: val1 = 310, val2 = 220Press any key to continue...
0 0