【问题标题】:Lambda expression C# failed to convert to the object type and fail with refLambda 表达式 C# 未能转换为对象类型并因 ref 失败
【发布时间】: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


【解决方案1】:

我不太确定你为什么要在这里使用 lambda,看起来你想要:

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.Length / str_encoded.Length)
            );

正如评论所指出的,您需要在格式字符串前加上 @ 前缀,因为它跨越多行。

【讨论】:

  • 我认为我们还需要在格式字符串上使用 @ 前缀,因为它似乎是一个多行字符串。
  • @lee,仅当字符串中有两个 (\) 斜杠或 (\") 时才需要 @ 筛选
  • @OlegOrlov 我不确定我是否理解您的最后评论...您正在谈论逐字字符串,对吗? msdn.microsoft.com/en-us/library/aa691090%28VS.71%29.aspx如果你的字符串是多行的(在源代码文件中,不是因为\r\n转义),你需要@前缀。
  • @weston 说得好,我知道将这两者混合在我的脑后很痒。
  • 从来不知道这个。但是,这会破坏他的\r\n,因为 `\` 不再是转义序列的开始。
【解决方案2】:

我同意 Lee 的观点,但是当您真的想创建这样的 Lamba 并获得它的输出时,您需要明确地转换如下内容:

(Func<int>)(() => (100 / str.Length) * (str.Length / str_encoded.Length)))();

我在玩线程时这样做,但不是在生产代码中

【讨论】:

  • 请更正括号。它们太多了......我假设,如果你使用 lambda 语句,你的 lambda 中必须有 return
  • @HamletHakobyan 谢谢,有些放错了地方。我更正了代码。
  • @HamletHakobyan 您不需要在 lambda 中使用 return;如果 lambda 具有导致值的表达式并分配给指示返回值的委托(例如 Func&lt;int&gt;),则暗示它
  • 在 lambda 表达式中没有,但在更正之前有 lambda 语句。
  • @HamletHakobyan 啊,是的,我在编辑中看到了;很好的接机。
【解决方案3】:

字符串常量可以用@ 前缀在多个代码行上定义,但是你的\r\n 将不起作用。因此,您可以将字符串片段与+ 一起添加以达到相同的效果:

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.Length / str_encoded.Length);}
        );
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多