static void Main(string[] args) { MyDele <int> deleAdd = new MyDele <int>(Program.Add); MyDele <double> deleMul = new MyDele <double>(Program.Mul); int ret = deleAdd(10, 10); double ret2 = deleMul(20, 20); }
static void NotMain() { MyBol Bol = (x, y) => x == y; MyBol_2 Bol_2 = (x, s) => s.Length > x; calculator C = (X, Y) => X * Y; VS S = () => Console.Write("我是无参数Labada表达式"); // int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int oddNumbers = numbers.Count(n => n % 2 == 1); // List <People> people = LoadData();//初始化 // 分解 // 自己建的委托 delegate bool MyDele(People p); // 使用委托 MyDele myDele = p => p.age > 20; // C#内置泛型委托 delegate TResult System.Func<int T, out TResult>(T arg) Func <People, bool> predicate = p => p.age > 20; IEnumerable <People> results2 = people.Where(predicate); // 委托的实例化:使用匿名方法 // < 委托类型 > < 实例化名 >= delegate (< 函数参数 >){ 函数体}; Func <People, bool> predicate2 = delegate(People p) { return(p.age > 20); }; IEnumerable <People> results4 = people.Where(predicate2); IEnumerable <People> results = people.Where(delegate(People p) { return(p.age > 20); }); // 更简便的写法 IEnumerable <People> results3 = people.Where(p => p.age > 20); }
static void GetReturnValue(IAsyncResult res) { AsyncResult ar = (AsyncResult)res; MyDele d = (MyDele)ar.AsyncDelegate; int kq = d.EndInvoke(res); Console.WriteLine(res.AsyncState.ToString()); Console.WriteLine("1+2={0}", kq); }
public static IEnumerable <int> GetNumbers(IEnumerable <int> numbers, MyDele gauntlet) { foreach (int num in numbers) { if (gauntlet(num)) { yield return(num); } } }
static void Main(string[] args) { //静态方法委托 MyDele dele1 = new MyDele(M1); Student stu = new Student(); //实例方法委托 dele1 += stu.SayHello; dele1.Invoke(); }
static void Main(string[] args) { MyDele <int> deleAdd = new MyDele <int>(Add); int res = deleAdd(100, 200); Console.WriteLine(res); MyDele <double> deleMul = new MyDele <double>(Mul); double mulRes = deleMul(3.0, 4.0); Console.WriteLine(mulRes); }
static void Main(string[] args) { MyDele dele1 = new MyDele(M1); int res1 = dele1(100, 200); Console.WriteLine(res1); dele1 += M2; int res2 = dele1(200, 300); Console.WriteLine(res2); }
static void Main(string[] args) { M1 m1 = new M1(); MyDele myDele = new MyDele(m1.Action); myDele += m1.Action; Console.WriteLine("++++++"); MyDele2 myDele2 = new MyDele2(M2.Add); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("Main thread : " + Thread.CurrentThread.GetHashCode()); MyDele d = new MyDele(Add); IAsyncResult isr = d.BeginInvoke(1, 2, null, null); Console.WriteLine("Main completed."); int kq = d.EndInvoke(isr); Console.WriteLine("1+2={0}", kq); Console.ReadLine(); }
private void AddMessage(string msg) { if (lBox_Msg.InvokeRequired) { MyDele dele = AddMessage; object[] objs = new object[] { msg }; lBox_Msg.BeginInvoke(dele, objs); } else { lBox_Msg.Items.Add(msg); } }
static void Main(string[] args) { Console.WriteLine("Main thread : " + Thread.CurrentThread.GetHashCode()); MyDele d = new MyDele(Add); AsyncCallback acb = new AsyncCallback(GetReturnValue); IAsyncResult isr = d.BeginInvoke(1, 2, acb, "CallBackTest"); //while (!isr.IsCompleted) //{ // Console.WriteLine("Doing more work in Main"); // Thread.Sleep(1000); //} Console.WriteLine("Main completed."); Console.ReadLine(); }
public static void main() { //自己声明的泛型委托为什么不用加参数类型约束呢? //Func是框架声明的,不知道你有声明参数,所以用func时要定义自己的参数类型约束 //使用var 可以省略func变量类型 MyDele <int> myDele = new MyDele <int>(add); var func = new Func <double, double, double>(add); func(1, 2); var action = new Action <long, int>(add); action(123123, 2); }
static void Main(string[] args) { MyDele <int> deleAdd = new MyDele <int>(Add); int result1 = deleAdd(100, 200); Console.WriteLine("The result is {0}.", result1); Console.WriteLine(deleAdd.GetType()); MyDele <double> deleMul = new MyDele <double>(Mul); double result2 = deleMul(300, 400); Console.WriteLine($"The result is {result2}."); Console.WriteLine(deleMul.GetType()); }
public void Run() { MyDele d = ReturnFive; d += ReturnTen; d += ReturnThree; //int value = d(); List <int> ints = new List <int>(); foreach (MyDele del in d.GetInvocationList()) { ints.Add(del()); } foreach (var item in ints) { Console.WriteLine(item); } }
static void Main(string[] args) { //Console.WriteLine("Hello World!"); //MyDele dele1 = new MyDele(M1); //dele1 += (new Student()).SayHello ; //dele1.Invoke(); //dele1(); MyDele dele = new MyDele(Add); int res = dele(100, 200); Console.WriteLine(res); MyDele <int> deleAdd = new MyDele <int>(Add); int result = deleAdd(100, 200); MyDele <double> deleMul = new MyDele <double>(Mul); double mulRes = deleMul(3.0, 4.0); Console.WriteLine(mulRes); Console.WriteLine(deleAdd.GetType().IsClass); //Action<string,string > action = new Action<string,string> (SayHello); //action("Tim","Mike"); Action <string, int> action = new Action <string, int>(SayHello); action("Tim", 3); Func <int, int, int> func = new Func <int, int, int>(Add); int res2 = func(100, 200); Console.WriteLine(res2); //lambda --anonymous,inline method var func1 = new Func <int, int, int>((a, b) => { return(a + b); }); int res3 = func(100, 200); func = (x, y) => { return(x * y); }; res3 = func(3, 4); Console.WriteLine(res3); DoSomeCalc((a, b) => { return(a * b); }, 100, 200); }
static void Main(string[] args) { Console.WriteLine("Main thread : " + Thread.CurrentThread.GetHashCode()); MyDele d = new MyDele(Add); AsyncCallback acb = new AsyncCallback(GetReturnValue); IAsyncResult isr = d.BeginInvoke(1, 2, (res) => { AsyncResult ar = (AsyncResult)res; MyDele dd = (MyDele)ar.AsyncDelegate; int kq = dd.EndInvoke(res); Console.WriteLine(res.AsyncState.ToString()); Console.WriteLine("1+2={0}", kq); }, "CallBackTest"); //while (!isr.IsCompleted) //{ // Console.WriteLine("Doing more work in Main"); // Thread.Sleep(1000); //} Console.WriteLine("Main completed."); Console.ReadLine(); }
static void Main(string[] args) { MyDele <double> dele = new MyDele <double>(mul); Console.WriteLine(dele(2.2, 3.2)); }
public static void main() { MyDele myDele = new MyDele(M1);
static void Main(string[] args) { MyDele dele1 = new MyDele(M1); Console.ReadLine(); }