Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9018409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:31:13+00:00 2026-06-16T04:31:13+00:00

I have the following code that doesn’t compile. I get the error Cannot use

  • 0

I have the following code that doesn’t compile. I get the error

Cannot use ref or out parameter ‘messageLockToken’ inside an anonymous method, lambda expression, or query expression

I admit I’m new to Lambdas and am a bit confused about resolving this one. Does anyone know how I can express the logic below in a compiler acceptable manner?

public T Receive<T>(TimeSpan receiveTimeout, out Guid messageLockToken)
{
    // do work
}

public Task<T> ReceiveAsync<T>(TimeSpan receiveTimeout, out Guid messageLockToken)
{
    Task<T> sendQueueMsgTask = new Task<T>(() => Receive<T>(receiveTimeout, out messageLockToken));
    return sendQueueMsgTask;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:31:16+00:00Added an answer on June 16, 2026 at 4:31 am

    First, your method wouldn’t work even if it compiled. That’s because when you use new Task(), you always have to call Start() on that Task (or, alternatively, use Task.Run()).

    Now, you can’t use the out parameter here, because it doesn’t make much sense. You want to return the Task from the method immediately, but that also means returning the Guid immediately. Which means there is no way for your call to Receive() to actually affect the value of the out parameter.

    I think using a Tuple (as Rawling suggested) or a custom return type is the way to go here:

    public Task<Tuple<T, Guid>> ReceiveAsync<T>(TimeSpan receiveTimeout)
    {
        return Task.Run(
            () =>
            {
                Guid messageLockToken;
                var result = Receive<T>(receiveTimeout, out messageLockToken);
                return Tuple.Create(result, messageLockToken);
            });
    }
    

    This assumes you actually want to do this, see Stephen Toub’s article Should I expose asynchronous wrappers for synchronous methods? for reasons why doing this is probably not a good idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code snippet that i use to compile class at the run
I have the following code that works but doesn't use my layout webscreen. @Override
I have the following code that doesn't compile. class Base { public: virtual ~Base()
I have following code that does not work: I never get to goToFoodDetail .
I have the following code that doesn't seem to be OK in context of
I have the following code that generates a compiler error: Boolean IConvertible.ToBoolean(IFormatProvider provider) {
I have the following code that works on Linux but doesn't work on Windows(VS2008)
Given the following code (that doesn't work): while True: # Snip: print out current
I have following code that does not work due to a being a value
I have the following code that when i change the drop down it doesnt

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.