public void Invoke() { ////定义委托,让委托指向方法 DelMath del0 = new DelMath(Add); DelMath del = Add; Console.WriteLine(Test(del)); //一个匿名委托的定义如下: DelMath m1 = new DelMath(delegate(int a, int b) { return(a * b); }); DelMath m2 = delegate(int a, int b) { return(a + b); }; DelMath m3 = (int i1, int i2) => (i1 + i2); DelMath m4 = (a, b) => a + b; string[] arr = { "DsDSd", "DSfdsdaDA", "dsaDSDSA", "FDFsdfsg" }; DelString delTrans = ToLower; StringProcess(arr, delTrans); string[] arr1 = { "sas", "sasfff", "hfgh" }; delTrans = ToUpper; StringProcess(arr1, delTrans); }
public static void Main(string[] args) { DelMath <int> objDel = NumberType; DelMath <float> objDel2 = NumberType; Console.WriteLine(objDel(10)); Console.WriteLine(objDel2(108.756F)); }
static void Snipeet7() { DelMath <int> objDel = Numbers2.NumberType; DelMath <float> objDel2 = Numbers2.NumberType; Console.WriteLine(objDel(10)); Console.WriteLine(objDel2(108.756F)); }
//声明委托 public void Math() { DelMath del = new DelMath(Add);//定义委托,将方法名作为参数传递给委托 // DelMath del = Add;//简单写法 Console.WriteLine(del(100, 2)); del += Add2;//为委托添加多个方法 //del += new DelMath(Add2);//为委托添加多个方法 Func<int, int, int> fun1 = Add; Action<int,int> act1=Add3; Console.WriteLine(del(100, 2));//Add、Add2两个方法都会执行 Console.WriteLine(TT(Add2, 100, 900));//将方法名作为参数传递给TT方法 }
private static int Test(DelMath del) { return(del(5, 6)); }
public void Invoke() { DelMath del1 = new DelMath(Add); int Sum = del1(12, 13); Console.WriteLine(Sum); DelMath del2 = Sub; Console.WriteLine(del2(13, 11)); DelMath del3 = delegate(int i1, int i2) { return(i1 / i2); }; Console.WriteLine(del3(12, 3)); DelMath del4 = (a, b) => a + b; Console.WriteLine(del4(123, 456)); Func <int, int> delFunc1 = delegate(int a) { return(++a); }; Func <int, int> delFunc2 = a => ++ a; DelMath delLambda0 = delegate(int i, int i1) { return(i + i1); }; delLambda0(3, 2); DelMath delLambda1 = (i, i1) => { return(i + i1); }; delLambda1(3, 2); DelMath delLambda2 = (i, i1) => i + i1; delLambda2(3, 2); Console.WriteLine(" 主线程是:{0}", Thread.CurrentThread.ManagedThreadId); DelMath myDel = new DelMath(AddStatic); //myDel(1,3) IAsyncResult delResult = myDel.BeginInvoke(3, 4, null, 2); while (!delResult.IsCompleted) { //主线程干其他事 } int addResult = myDel.EndInvoke(delResult); Console.WriteLine("主线程获得结果是:{0}", addResult); myDelMath1 mdm = delegate(int a, int b, int c) { return(a + b + c); }; Console.WriteLine(mdm(12, 13, 16)); }
//其中一个参数是委托 public int TT(DelMath del, int x, int y) { return del(x, y); }