【发布时间】:2013-01-06 17:58:30
【问题描述】:
有这样的功能:
private static void EncodeString(ref string str)
{
using (RLE inst_rle = new RLE())
{
string str_encoded = inst_rle.Encode(ref str);
Console.WriteLine(
"\r\nBase string ({0} chars): {1}\r\nAfter RLE-encoding ({2} chars): {3}\r\nCompression percentage: %{4}",
str.Length, str, str_encoded.Length, str_encoded,
() => { (100 * (str.Length - str.encoded.Length) / str.Length); }
);
}
}
我记得这是 C# 中的一种 lambdas 风格: () => { ; }
但是遇到这样的错误:
- 无法将 lambda 表达式转换为类型“对象”,因为它
- 只有赋值、调用、递增、递减和新对象表达式可以作为语句使用
- 不能在匿名方法、lambda 表达式或查询表达式中使用 ref 或 out 参数“str”
- 不能在匿名方法、lambda 表达式或查询表达式中使用 ref 或 out 参数“str”
如何在我的应用程序(控制台应用程序)中使用 C# EXACLTY 中的 Lambda 而无需明确使用
Delegate / Func,比如() => { } 方式?
【问题讨论】:
-
仅供参考,我认为没有任何答案表明您不能在任何 lambdas 中使用您的
str参数。由于str标有ref,因此您不能在lambda 中使用它。在 lambda 中使用之前,您必须先将其存储为局部临时变量。正如 Lee 指出的那样,无论如何,我认为在这种情况下根本没有理由使用 lambda。 -
@ChrisSinclair 不存在使用 lambda 引用的任何变态?
-
抱歉,我不明白你的意思,奥列格。你能改写你的问题吗?
-
@ChrisSinclair 我的意思是:stackoverflow.com/questions/14185982/…
标签: c# linq lambda expression