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?

4
  • 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. Commented Jan 6, 2013 at 18:37
  • @ChrisSinclair Doesn't exist any perversion to use references with lambdas? Commented Jan 6, 2013 at 19:21
  • Sorry, I don't understand what you mean, Oleg. Can you rephrase your question please? Commented Jan 6, 2013 at 20:28
  • @ChrisSinclair I mean this: stackoverflow.com/questions/14185982/… Commented Jan 6, 2013 at 20:31

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I think we'll also need to use the @ prefix on the format string since it appears to be a multi-line string.
@lee, @ screening is needed only if you have two (\) slashes in string or (\")
@OlegOrlov I'm not sure I understand your last comment... You are talking about verbatim strings, right? 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.
@weston Good point, knew something was itching at the back of my head about mixing those two.
Never knew this. However, this breaks his \r\n as `\` is no longer the start of an escape sequence.
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

5 Comments

Please correct your brackets. They are so many... And i assume, if you use lambda statement you must have return in your lambda.
@HamletHakobyan thanks, had some misplaced. I corrected the code.
@HamletHakobyan You do not need a return in the lambda; it is implied if the lambda has an expression that results in a value and is assigned to a delegate that indicates a return value (such as Func<int>)
In lambda expression not, but before correction there was lambda statement.
@HamletHakobyan Ahh yeah, I see that in the edits; good pick up.
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);}
        );
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.