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

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);
}
}
主站蜘蛛池模板: 常宁市| 长武县| 安丘市| 陇南市| 湟源县| 溆浦县| 静安区| 拉萨市| 莱芜市| 正蓝旗| 雷波县| 宜春市| 河津市| 温宿县| 赣州市| 方山县| 长寿区| 介休市| 晋城| 凤凰县| 容城县| 宁津县| 陕西省| 曲阳县| 绍兴市| 广宁县| 祥云县| 且末县| 肥乡县| 张家港市| 凤凰县| 咸丰县| 威海市| 延边| 广灵县| 佳木斯市| 黄石市| 大悟县| 虞城县| 武义县| 耿马|