private void ShowImageEventHandler(Mat img) { if (base.Width > 0 && base.Height > 0 && this._isDrawPicture) { this._isDrawPicture = false; this._frameImage = img; //delegate //{ // DirectShowImageBase expr_06 = this._directShowImageBase; // if (expr_06 == null) // { // return; // } // expr_06.Render(this._frameImage.GetData(new int[0]), this._frameImage.Cols, this._frameImage.Height, this._frameImage.Step); //}.BeginInvoke(delegate(IAsyncResult ar) MyDel1 t = delegate { DirectShowImageBase expr_06 = this._directShowImageBase; if (expr_06 == null) { return; } expr_06.Render(this._frameImage.GetData(new int[0]), this._frameImage.Cols, this._frameImage.Height, this._frameImage.Step); }; t.BeginInvoke(delegate(IAsyncResult ar) { this._isDrawPicture = true; }, null); } }
static void Main(string[] args) { MyDel1 avg = (a, b, c) => ((double)a + b + c) / 3.0; MyDel2 Add, Sub, Mul, Div; Add = (a, b) => a + b; Mul = (a, b) => a * b; Sub = (a, b) => a - b; Div = (a, b) => a / b; Random random = new Random(); MyDel3 rndnum = () => random.Next(); MyDel3[] arr = { rndnum, rndnum, rndnum, rndnum, rndnum }; MyDel4 myDel4 = a => { double sum = 0.0; foreach (var f in a) { sum += f(); } return(sum / a.Length); }; myDel4(arr); }
static void Main(string[] args) { MyDel1 twice = delegate() { Console.WriteLine("called MyFunc1: "); }; // // instead of // // static void MyFunc1() { Console.Write("called MyFunc1: "); } // MyDel1 twice = new MyDel1(MyFunc1); twice(); }
static void Main(string[] args) { { MyDel1 del = new MyDel1(Func1); del("Hello1"); } { MyDel1 del = Func1; // delegate inference (pass the method name, the delegate type would be inferred) del("Hello2"); } }
private void button5_Click(object sender, EventArgs e) { //实例化对象 MyInstObj myInstObj = new MyInstObj(); //创建委托并保存引用 MyDel1 delA = myInstObj.MyM1; MyDel1 delB = MyInstObj.OtherM2; delA += MyInstObj.OtherM2; delA -= myInstObj.MyM1;//删除实例方法 delA(1); }
/// <summary> /// 解密 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string UEncryption(string str) { System.Text.StringBuilder sbtext = new System.Text.StringBuilder(); for (int i = 0; i < str.Length; i++) { string c = str[i].ToString(); string cs = string.Empty; MyDel1 m1 = new MyDel1(UGetBase1); cs = m1(c, i); sbtext.Append(cs); } return(sbtext.ToString()); }
private void button4_Click(object sender, EventArgs e) { MyDel1 delA, delB; //实例化对象 MyInstObj myInstObj = new MyInstObj(); //创建委托引用 - 第二种方法 delA = myInstObj.MyM1; delB = MyInstObj.OtherM2; MyDel1 delC = delA + delB; delC(1); }
static void Main(string[] args) { //MyDel1 d1 = Func1; //bool b1 = d1(6, "lcy"); //匿名方法 MyDel1 d2 = delegate(int n1, string s1) { Console.WriteLine($"我是匿名方法:i={n1},s={s1}"); return(true); }; bool b1 = d2(6, "lcy"); Console.WriteLine(b1); Console.ReadKey(); }
static void Main() { int a = 29846; int[] arr = { 83, 26, 48, 72, 56, 92, 65, 82, 56, 19 }; MyDel1 d1 = GetDigits; MyDel2 d2 = PrintArray; d2(d1(a)); d2(arr); Console.WriteLine(); Console.WriteLine(d1.Method); Console.WriteLine(d1.Target); Console.WriteLine(d2.Method); Console.WriteLine(d2.Target); }
private void button2_Click(object sender, EventArgs e) { MyDel1 delVar, dVal; //实例化对象 MyInstObj myInstObj = new MyInstObj(); delVar = new MyDel1(myInstObj.MyM1); //实例方法 dVal = new MyDel1(MyInstObj.OtherM2); //静态方法 //创建委托引用并保存-第二种方法 delVar = myInstObj.MyM1; //实例方法 dVal = MyInstObj.OtherM2; //静态方法 // 第二种方法是一种快捷方法,它仅仅由方法说明符组成。和方法一的代码是等价的。这种快捷语法能够工作是因为在方法名称和其相应的委托类型之间存在隐式转换 //调用 delVar(1); dVal(2); }
static void Main(string[] args) { { Program p = new Program(); MyDel1 del1 = new MyDel1(p.Func1); del1(); } { MyDel1 del1 = delegate() { Console.Write("called Func1: "); }; del1(); } { //(() { Console.Write("called Func1: "); }) (); } }
static void Main(string[] args) { MyClass myClass = new MyClass(); MyTeam myTeam = new MyTeam(); MyDel1 myDel1 = myClass.AddNumber; MyDel2 myDel2 = MyClass.Print; myDel2 += myClass.Add; myDel2 += MyClass.Print; myDel1(20); myDel2(); myDel1(20); myDel2(); myDel1 += myTeam.AddNumber; myDel2 += myTeam.Add; myDel2 += myTeam.Print; Console.WriteLine(); myDel1(20); myDel2(); myDel1(20); myDel2(); //lambda MyDel3 myDel3 = (x) => { myTeam.TeamCount += x; Console.WriteLine(myTeam.TeamCount); return(myTeam.TeamCount); }; Console.WriteLine($"myDel3 : {myDel3(1000)}"); }