- Beginning C# 7 Hands-On:Advanced Language Features
- Tom Owsiak
- 321字
- 2021-07-02 15:29:20
Assigning functions to represent the delegate
For the right-hand side, we first need to start assigning the functions that it represents. To do this, we can say the following, for example, below the closed curly brace after this line:
public void FindSum(double x, double y)
Imagine that the first thing you'll do is to find the sum of two values. So, you say, for example, Find Sum and then double x and double y.
Then, you can update the labels; so, enter the following between a set of curly braces below this line:
sampLabel.Text = $"<br>{x}+{y}={x + y}";
Now, you can assign FindSum to <int> in the preceding line. You can set this equal, as follows:
Summarize<double> s = FindSum;
Of course, there are many other operations that you can perform. So, let's take this code: this basic function that adds, and define some other operations. Copy (Ctrl + C) these two lines and paste (Ctrl + V) them down below. This time, change FindSum to FindRatio and basically follow the same plan. We'll apply a += operator to make sure that it's appending. Then, to make a new line, put a <br> tag in there and, instead of x + y, change these to x / y. Of course, here you'd have to ensure that y is not 0. We can figure that out:
public void FindRatio(decimal x, decimal y) {
sampLabel.Text += $"<br>{x}/{y}={x / y}"; }
Let's do one more. So again, copy these two lines and paste them down below. This time, change FindRatio to FindProduct, which is the result of multiplying two values, and change x / y to x * y.
public void FindProduct(decimal x, decimal y)
{
sampLabel.Text += $"<br>{x}*{y}={x * y}";
}
Reminder: If it's brown (Windows) or orange (Mac) it shows on the screen exactly as it is.
- Getting Started with Citrix XenApp? 7.6
- Objective-C應(yīng)用開(kāi)發(fā)全程實(shí)錄
- Building a Game with Unity and Blender
- 深入理解Django:框架內(nèi)幕與實(shí)現(xiàn)原理
- 劍指Offer(專項(xiàng)突破版):數(shù)據(jù)結(jié)構(gòu)與算法名企面試題精講
- Practical DevOps
- Object-Oriented JavaScript(Second Edition)
- 深度強(qiáng)化學(xué)習(xí)算法與實(shí)踐:基于PyTorch的實(shí)現(xiàn)
- WebRTC技術(shù)詳解:從0到1構(gòu)建多人視頻會(huì)議系統(tǒng)
- Swift Playgrounds少兒趣編程
- Visual Basic程序設(shè)計(jì)
- 深入剖析Java虛擬機(jī):源碼剖析與實(shí)例詳解(基礎(chǔ)卷)
- Clojure Polymorphism
- Mastering Bootstrap 4
- Beginning C# 7 Hands-On:The Core Language