码迷,mamicode.com
首页 > Windows程序 > 详细

C#委托与回调的应用体会(一)

时间:2018-10-13 22:54:16      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:vat   eve   地址   initial   函数   sel   form   ace   obj   

在实现定积分通用函数计算程序设计过程中,第一次使用C#的委托特性,个人感觉C#的委托有点类似指针,就是把一个方法做成地址传递过去。尽管这种理解不一定严谨,但是取得的效果确实差不多。

以下便是委托的实际应用~

namespace functionTheOne
{
public partial class Form1 : Form
{
private delegate double Function(double x);
private Function function;
public Form1()
{
InitializeComponent();
}

private void label1_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
if (radioButton3.Checked == true)
{
double a; double b; double z;
a = Convert.ToDouble(textBox1.Text);
b = Convert.ToDouble(textBox2.Text);
function = new Function(jishuan1);
hanshu(a, b, function);
}
if (radioButton2.Checked == true)
{
double a; double b; double z;
a = Convert.ToDouble(textBox1.Text);
b = Convert.ToDouble(textBox2.Text);
function = new Function(jishuan2);
hanshu(a, b, function);
}
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{

}

private void label3_Click(object sender, EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{

}
private double hanshu(double a,double b,Function ch)//计算通用的函数
{
double sum=0;
for(int i = 0; i < 10000; i++)
{
sum = sum + (a-b)/10000*(ch(((a - b) / 10000 )*i+b)); //a是上限,b是下限
}
textBox3.Text = Convert.ToString(sum);
return sum;
}
private double jishuan1(double x )
{
return x;
}
private double jishuan2(double x)
{
return x * x;
}
}

}

C#委托与回调的应用体会(一)

标签:vat   eve   地址   initial   函数   sel   form   ace   obj   

原文地址:https://www.cnblogs.com/fly0512/p/9784328.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!