- Beginning C# 7 Hands-On:Advanced Language Features
- Tom Owsiak
- 202字
- 2021-07-02 15:29:26
Chapter review
For review, the complete version of the Default.aspx.cs file for this chapter, including comments, is shown in the following code block:
using System;//needed for array, Convert, and Converter
delegate double CompareValues(double x, double y);//delegate for defining expression bodied lambda
public partial class _Default :System.Web.UI.Page
{
double FromStringToDouble(string s) => Convert.ToDouble(s);//expression bodied function member
protected void Button1_Click(object sender, EventArgs e)
{
//split entries into array of strings
string[] vals = TextBox1.Text.Split(new char[] { ',' });
//line 10 below converts all strings to doubles using the
//vals array, and a new Converter object
//which really just calls FromStringToDouble
double[] doubleVals =
Array.ConvertAll(vals, new Converter<string, double>(FromStringToDouble));
//lines 13-17 define the expression bodied lambda, this one compares two
//values and returns the bigger
CompareValues compareValues = (xin, yin) =>
{
double x = xin, y = yin;
return x > y ? x : y;
};
//line 19 invokes CompareValuesInList, which needs the lambda, and
//list of values to compare
sampLabel.Text =
CompareValuesInList(compareValues, doubleVals[0], doubleVals[1],doubleVals[2]).ToString();
}
//lines 22-25 below return either third value if it's biggest,
//or one of the other two
static double CompareValuesInList(CompareValues compFirstTwo, double first, double second, double third)
{
return third > compFirstTwo(first, second) ? third : compFirstTwo(first, second);
}
}
推薦閱讀
- Cocos2d-x游戲開發:手把手教你Lua語言的編程方法
- 可解釋機器學習:模型、方法與實踐
- Angular開發入門與實戰
- Go語言編程
- Solutions Architect's Handbook
- TypeScript 2.x By Example
- 算法設計與分析:基于C++編程語言的描述
- AI自動化測試:技術原理、平臺搭建與工程實踐
- JavaWeb從入門到精通(視頻實戰版)
- 第五空間戰略:大國間的網絡博弈
- 零基礎學西門子PLC編程:入門、提高、應用、實例
- 數據分析從入門到進階
- Python量化交易實戰:使用vn.py構建交易系統
- Node.js Web Development(Third Edition)
- Hands-On Android UI Development