- Beginning C# 7 Hands-On:Advanced Language Features
- Tom Owsiak
- 250字
- 2021-07-02 15:29:24
Chapter review
For review purposes, the complete version of the Default.aspx.cs file for this chapter, 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 bool Compare(double x, double y);
public delegate double Multiply(double x, double y);
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
double x = 10, y = 25; //declare two variables
//the two variables are accessible inside the lambda expressions
Compare comp = (a, b) => (a == b);//define comparison lambda
//invoke the lambda in the line below
sampLabel.Text =
$"{x} and {y} are equal is {comp(x, y).ToString().ToLower()}";
//line define a lambda for multiplication
Multiply mult = (a, b) => (a * b);
//invoke the multiplication lambda
sampLabel.Text += $"<br>{x}*{y} is {mult(x, y)}";
//make array of doubles
double[] dubsArray = new double[] { 1, 2, 3, 4, 5 };
//actions encapsulate functions that do not return a value
//but actions can accept arguments to operate on
Action<double> showDouble =
(a) => sampLabel.Text += "<br>" + (a * a);
//it's now possible to perform the action on each d repeatedly
foreach (var d in dubsArray)
{
showDouble(d);
}
}
}
推薦閱讀
- scikit-learn Cookbook
- Data Visualization with D3 4.x Cookbook(Second Edition)
- iOS面試一戰(zhàn)到底
- 自己動(dòng)手實(shí)現(xiàn)Lua:虛擬機(jī)、編譯器和標(biāo)準(zhǔn)庫
- Java 9 Programming Blueprints
- Python零基礎(chǔ)快樂學(xué)習(xí)之旅(K12實(shí)戰(zhàn)訓(xùn)練)
- Django:Web Development with Python
- The Data Visualization Workshop
- Scala程序員面試算法寶典
- Java系統(tǒng)化項(xiàng)目開發(fā)教程
- jQuery炫酷應(yīng)用實(shí)例集錦
- Couchbase Essentials
- Django實(shí)戰(zhàn):Python Web典型模塊與項(xiàng)目開發(fā)
- 動(dòng)手打造深度學(xué)習(xí)框架
- Advanced UFT 12 for Test Engineers Cookbook