C# - 委托_ 匿名方法

代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace 委托_例子
 8 {
 9     static class Program
10     {
11         // 定义委托(Double类型)
12         delegate double Integand(double x);
13
14         //定义方法
15         static double Method1(double x)
16         {
17             return 2 * x + 1;
18         }
19
20         static double Method2(double x)
21         {
22             return x * x;
23         }
24
25         // 定义是用委托的方法
26         static double DefiniteIntegrate(Integand f, double a, double b)
27         {
28             const int sect = 1000;
29
30             double area = 0;
31
32             double delta = (b - a) / sect;
33
34             for (int i = 0; i < sect; i++)
35             {
36                 //此处的 f 就代表 Method1 或是 Method2。
37                 //传给他们一个double类型的值,返回一个double类型的值。
38                 //此时的double值就是长乘以宽“a + i * delta”
39                 area += delta * f(a + i * delta);
40             }
41
42             return area;
43         }
44
45         //利用【匿名方法】,可以直接把代码块定义为委托,而不需要事先定义方法。
46         //不光可以使用代码块定义变量,而且可以使用代码块外面的变量,即主方法中的变量。
47         static void Main(string[] args)
48         {
49             int a = 1;
50
51             int b = 5;
52
53             Integand i = delegate(double x)
54             {
55                 return a * x + b;
56             };  //添加分号
57
58             double result = DefiniteIntegrate(i, a, b);
59
60             Console.WriteLine("result:{0}", result);
61
62             Console.ReadKey();
63         }
64     }
65 }
时间: 2024-07-31 18:55:37

C# - 委托_ 匿名方法的相关文章

C# 匿名委托、匿名方法、匿名对象、Lambda表达式

一.匿名类型可通过使用 new 运算符和对象初始值创建匿名类型.示例:var v = new { Name = "Micro", Message = "Hello" };var v = new[] {     new { Name = "Micro", Message = "Hello" },     new { Name = "Soft", Message = "Wold!" }};匿

多播委托和匿名方法再加上Lambda表达式

多播委托就是好几个方法全都委托给一个委托变量 代码: 1 namespace 委托 2 { 3 class Program 4 { 5 static void math1() 6 { 7 Console.WriteLine("这是第一个方法"); 8 } 9 10 static void math2() 11 { 12 Console.WriteLine("这是第二个方法"); 13 } 14 15 static void Main(string[] args) 1

委托三------------多播委托与匿名方法

============================================多播委托 -------------------------------------主程序 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication3 {     class Pr

C#中分别对委托、匿名方法、Lambda表达式、Lambda表达式树以及反射执行同一方法的过程进行比较。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; using System.Linq.Expressions; namespace INPEXOne.LearnCS { class RefletLambdaDelegate { static object[] para

五分钟重温C#委托,匿名方法,Lambda,泛型委托,表达式树

五分钟重温C#委托,匿名方法,Lambda,泛型委托,表达式树 https://masuit.com/81 曾经新生代,好多都经过漫长的学习,理解,实践才能掌握委托,表达式树这些应用.今天我尝试用简单的方法叙述一下,让大家在五分钟内看完这篇博客 第一分钟:委托 有些教材,博客说到委托都会提到事件,虽然事件是委托的一个实例,但是为了理解起来更简单,今天只谈委托不谈事件.先上一段代码: 下边的代码,完成了一个委托应用的演示.一个委托分三个步骤: 1 2 3 4 5 6 7 8 9 10 11 12

.net基础扫盲-小例子串委托、匿名方法、lamuda表达式

我把委托理解为:委托是一种方法的格式,当然他也是一种类型. 只要是方法的格式跟委托定义的格式是一样的,那么就可以把该方法附加给该委托.看以下demo 声明委托: <p style="margin:0in;font-family:微软雅黑;font-size:12.0pt"></p><pre name="code" class="csharp">public delegate void Add(int a,int

从委托、匿名方法到Lambda

前面讲过委托的知识,本次由委托过渡到Lambda表达式,更易于理解. 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 int[] intA = { 1, 3, 5, 7 }; 6 ProcArray(intA, AddOne); 7 foreach (int i in intA) 8 { 9 Console.Write(i + " "); 10 } 11 12 Console.ReadKey(); 13 } 14

委托、匿名方法、Lambda表达式的演进

假设给我们一个泛型对象List<T>,T为int类型,要求我们使用该对象方法FindAll(Predicate<T> match)从中找出该List中的偶数,您如何实现? 说明一下:Predicate<T>是一个泛型委托,它的原型为public delegate bool Predicate<T>(T obj),该委托传入一个T类型对象,经逻辑判断后返回布尔值. 委托 可能您首先想到的是用委托实现,实现方法如下: //方法1staticvoid Method

委托,匿名方法,Lambda,泛型委托,表达式树

一.委托:完成一个委托应分三个步骤://step01:首先用delegate定义一个委托;public delegate int CalculatorAdd(int x, int y);//step02:声明一个方法来对应委托.public int Add(int x, int y){ return x + y;}protected void FrmTest_Load(object sender, EventArgs e){ //step03:用这个方法来实例化这个委托. CalculatorA