官术网_书友最值得收藏!

Calling the delegate

Now, we have to stack up these calls; so, enter the following beneath the Summarize<double> s = FindSum; line:

s += FindRatio;
s += FindProduct;

Note that you put the next function name, FindRatio, and then the next line will be FindProduct.

Then, of course, to call it, enter the following on the very next line:

s(4, 5); 

This is how you would invoke that delegate: you will call it, specify the name, and then pass in those values of 4 and 5.

The complete version of the Default.aspx.cs file for the double data type, including comments, is shown in the following code block:

//using is a directive
//System is a name space
//name space is a collection of features that our needs to run
using System;
//public means accessible anywhere
//partial means this class is split over multiple files
//class is a keyword and think of it as the outermost level of grouping
//:System.Web.UI.Page means our page inherits the features of a Page
public delegate void Summarize<T>(T x, T y);//declare generic delegate
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Summarize<decimal> s = FindSum;//assign FindSum to the delegate
s += FindRatio;//assign FindRatio to the delegate
s += FindProduct;//assign FindProduct to the delegate
s(4, 5);//invoke the delegate, causing the chain of functions to be executed
}
public void FindSum(decimal x, decimal y)
{
sampLabel.Text = $"<br>{x}+{y}={x + y}";
}
public void FindRatio(decimal x, decimal y)
{
sampLabel.Text += $"<br>{x}/{y}={x / y}";
}
public void FindProduct(decimal x, decimal y)
{
sampLabel.Text += $"<br>{x}*{y}={x * y}";
}
}
主站蜘蛛池模板: 罗甸县| 宁南县| 隆林| 本溪| 黎川县| 英吉沙县| 瓮安县| 景谷| 明溪县| 永善县| 榆社县| 兖州市| 冀州市| 中超| 张家港市| 钟祥市| 砚山县| 化州市| 栾城县| 吉林市| 当阳市| 武定县| 碌曲县| 理塘县| 小金县| 全南县| 平顶山市| 奉节县| 蕲春县| 海兴县| 新竹市| 古丈县| 安康市| 祁阳县| 宁蒗| 宣威市| 平邑县| 中阳县| 新津县| 洛浦县| 青田县|