(Lambda 函数可能是也可能不是我正在寻找的,我不确定)
基本上我想要完成的是:
int areaOfRectangle = (int x, int y) => {return x * y;};
但它给出了错误:“无法将 lambda 表达式转换为类型'int',因为它不是委托类型”
更详细的问题(实际上与问题无关,但我知道有人会问)是:
我有几个从重写的 OnLayout 分支的函数以及每个函数都依赖的多个函数。为了可读性和为以后的扩展设置先例,我希望从 OnLayout 分支的函数看起来都相似。为此,我需要对它们进行划分并尽可能重用命名:
protected override void OnLayout(LayoutEventArgs levent)
switch (LayoutShape)
{
case (square):
doSquareLayout();
break;
case (round):
doRoundLayout();
break;
etc..
etc..
}
void doSquareLayout()
{
Region layerShape = (int Layer) =>
{
//do some calculation
return new Region(Math.Ceiling(Math.Sqrt(ItemCount)));
}
int gradientAngle = (int itemIndex) =>
{
//do some calculation
return ret;
}
//Common-ish layout code that uses layerShape and gradientAngle goes here
}
void doRoundLayout()
{
Region layerShape = (int Layer) =>
{
//Do some calculation
GraphicsPath shape = new GraphicsPath();
shape.AddEllipse(0, 0, Width, Height);
return new Region(shape);
}
int gradientAngle = (int itemIndex) =>
{
//do some calculation
return ret;
}
//Common-ish layout code that uses layerShape and gradientAngle goes here
}
我现在找到的所有示例都说您必须声明一个委托,但我知道我已经看到了一个单行 lambda 声明......