0

Have such function:

   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); }

                             );
        }
    }

As I remember it's a style of lambdas in C#: () => { < action > ; }

But getting such errors:

  • Cannot convert lambda expression to type 'object' because it
  • Only assignment, call, increment, decrement, and new object expressions can be used as a statement
  • Cannot use ref or out parameter 'str' inside an anonymous method, lambda expression, or query expression
  • Cannot use ref or out parameter 'str' inside an anonymous method, lambda expression, or query expression

How to use Lambda in C# EXACLTY in my app (console app) without explicity using

Delegate / Func< T >, like in () => { } way?

Secret
  • 2,627
  • 7
  • 32
  • 46
  • 1
    FYI, I don't think any of the answers have picked up that you can't use your `str` parameter in any of the lambdas. As `str` is marked with `ref`, you can't use it in the lambda. You would have to store it as a local temporary variable first before using in the lambda. As Lee pointed out though, I see no reason to be using a lambda at all in this case anyway. – Chris Sinclair Jan 06 '13 at 18:37
  • @ChrisSinclair Doesn't exist any perversion to use references with lambdas? – Secret Jan 06 '13 at 19:21
  • Sorry, I don't understand what you mean, Oleg. Can you rephrase your question please? – Chris Sinclair Jan 06 '13 at 20:28
  • @ChrisSinclair I mean this: http://stackoverflow.com/questions/14185982/perversion-does-exist-any-way-to-natively-use-refences-with-lambdas-in-c#comment19663378_14185982 – Secret Jan 06 '13 at 20:31

3 Answers3

2

I'm not really sure why you want to use a lambda here, it looks like you want:

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)
            );

As the comment points out, you need to prefix the format string with an @ since it spans multiple lines.

Lee
  • 142,018
  • 20
  • 234
  • 287
  • I think we'll also need to use the `@` prefix on the format string since it appears to be a multi-line string. – Chris Sinclair Jan 06 '13 at 18:06
  • @lee, @ screening is needed only if you have two (\\) slashes in string or (\") – Secret Jan 06 '13 at 18:16
  • @OlegOrlov I'm not sure I understand your last comment... You _are_ talking about verbatim strings, right? http://msdn.microsoft.com/en-us/library/aa691090%28VS.71%29.aspx If your string is multi-line (in the souce code file, not because of \r\n escapes), you need the @ prefix. – Chris Sinclair Jan 06 '13 at 18:28
  • @weston Good point, knew something was itching at the back of my head about mixing those two. – Chris Sinclair Jan 06 '13 at 18:33
  • Never knew this. However, this breaks his `\r\n` as `\\` is no longer the start of an escape sequence. – weston Jan 06 '13 at 18:35
1

I agree with Lee, but when you really want to create a Lamba like this, and get its output you need to cast explicitly something like:

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

I do this when I am playing with threads, not in production code though

MBen
  • 3,956
  • 21
  • 25
1

String constants can be defined over multiple code lines with a @ prefix, but then your \r\n will not work. So instead you can add string framgents together with + to achieve the same effect:

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);}
        );
    }
}
weston
  • 54,145
  • 21
  • 145
  • 203