C# - 委托_求定积分通用方法
2015-05-24 15:46
381 查看
代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 委托_例子 { static class Program { // 定义委托(Double类型) delegate double Integand(double x); //定义方法 static double Method1(double x) { return 2 * x + 1; } static double Method2(double x) { return x * x; } // 定义是用委托的方法 static double DefiniteIntegrate(Integand f, double a, double b) { const int sect = 1000; double area = 0; double delta = (b - a) / sect; for (int i = 0; i < sect; i++) { //此处的 f 就代表 Method1 或是 Method2。 //传给他们一个double类型的值,返回一个double类型的值。 //此时的double值就是长乘以宽“a + i * delta” area += delta * f(a + i * delta); } return area; } static void Main(string[] args) { //调用方法 //传入一个方法 Console.WriteLine(Program.DefiniteIntegrate(Method1, 1, 5)); Console.WriteLine(); Console.WriteLine(Program.DefiniteIntegrate(Method2, 0, 1)); Console.ReadKey(); } } }
相关文章推荐
- Atitit java方法引用(Method References) 与c#委托与脚本语言js的函数指针
- C#用Lambda和委托实现模板方法
- C# 委托系列(二)将方法绑定到委托
- C#笔记 委托 事件 匿名方法 Action
- [C#] 委托与方法组
- c# 扩展方法奇思妙用基础篇七:IsBetween 通用扩展
- Asp.Net中C#实现的DataGrid小计,合计和纵向合并的通用方法
- JAVA通过反映方法实现C#的委托
- 【转】C#中的委托,匿名方法和Lambda表达式
- C# 中函数内定义函数的委托方法
- C#通用类型转换方法
- c# 多播委托 和匿名方法
- 最简单的C#委托实现方法
- C# ASP.NET 最常用的通用权限的3个方法例子展示(每个功能一行代码实现)
- C#学习之初步理解委托、事件、匿名方法和Lambda
- c# 扩展方法奇思妙用基础:IsBetween 通用扩展
- C#方法中的反射方式和委托方式(小实例)
- c#匿名方法调用委托
- [C#] 委托与方法组
- C#的委托,匿名方法和Lambda表达式