无法将 lambda 表达式转换为 bool 类型
Cannot convert lambda expression to type bool
无法将 lambda 表达式转换为 bool 类型,因为它不是委托类型。
protected void button1_Click(object sender, EventArgs e)
{
int totalpoints;
Int32 realpoints;
lelpoints = 0;
totalpoints = Convert.ToInt32(dPts.Text);
totalpoints = totalpoints + 1;
totalpoints = realpoints;
dPts.Text = totalpoints.ToString();
}
protected void buyBG_Click(object sender, EventArgs e)
{
string[] rbg = new string[] { "red", "green", "blue" };
Random random = new Random();
int randomNumber = random.Next(0, 3);
string currentbg = rbg[randomNumber];
if (realpoints => 10 ){}
这是我在使用上面提供的代码时遇到的问题。问题显示在底部的 If 语句中。
编辑:将 =>
更改为 >=
确实解决了该问题,但现在报告错误 "the name realpoints does not exist in the current context"。谢谢
不是 =>
,而是 >=
。
这应该可以解决您的问题。
=>
是一个 lambda 运算符与大于或等于运算符 >=
不同
您可以将 =>(lambda 符号)读作 "goes to"。意味着您正在将参数传递给委托主体。在你的例子中,realpoints => 10 意味着 realpoints 是参数,10 是一个主体,这个主体不是 bool 类型(10 不是 bool)。
例如,您有一个整数列表,例如:
List intList = new List {10, 14,5,17};
你想在列表中找到第一个大于 10 的整数,然后你可以使用 lambda 表达式,比如
int numberFound = intList.Find(x => x > 10 );
谢谢,
苏克
无法将 lambda 表达式转换为 bool 类型,因为它不是委托类型。
protected void button1_Click(object sender, EventArgs e)
{
int totalpoints;
Int32 realpoints;
lelpoints = 0;
totalpoints = Convert.ToInt32(dPts.Text);
totalpoints = totalpoints + 1;
totalpoints = realpoints;
dPts.Text = totalpoints.ToString();
}
protected void buyBG_Click(object sender, EventArgs e)
{
string[] rbg = new string[] { "red", "green", "blue" };
Random random = new Random();
int randomNumber = random.Next(0, 3);
string currentbg = rbg[randomNumber];
if (realpoints => 10 ){}
这是我在使用上面提供的代码时遇到的问题。问题显示在底部的 If 语句中。
编辑:将 =>
更改为 >=
确实解决了该问题,但现在报告错误 "the name realpoints does not exist in the current context"。谢谢
不是 =>
,而是 >=
。
这应该可以解决您的问题。
=>
是一个 lambda 运算符与大于或等于运算符 >=
您可以将 =>(lambda 符号)读作 "goes to"。意味着您正在将参数传递给委托主体。在你的例子中,realpoints => 10 意味着 realpoints 是参数,10 是一个主体,这个主体不是 bool 类型(10 不是 bool)。
例如,您有一个整数列表,例如: List intList = new List {10, 14,5,17};
你想在列表中找到第一个大于 10 的整数,然后你可以使用 lambda 表达式,比如 int numberFound = intList.Find(x => x > 10 );
谢谢, 苏克