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

  • Home
  • SEARCH
  • 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 8798403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:05:26+00:00 2026-06-14T00:05:26+00:00

I bumped into this C# compiler error and while I’m ok with the compiler

  • 0

I bumped into this C# compiler error and while I’m ok with the compiler not being able to infer the type of the lambda the error message seems wrong. Here’s the relevant piece of code:

Func<object> lambda = async () => { return await Task.FromResult(1); };

and here’s the compiler error:

error CS4010: Cannot convert async lambda expression to delegate type ‘System.Func<object>‘. An async lambda expression may return void, Task or Task<T>, none of which are convertible to ‘System.Func<object>‘.

The part I don’t quite understand is the last sentence. The lambda is indeed returning Task<int>, but why does the compiler think that it should try to convert it to System.Func<object>?

On the other hand, if what the error message is trying to convey is that System.Func<Task<int>> cannot be assigned to System.Func<object>, that doesn’t seem to be the case thanks to generic delegate covariance which means that this works just fine instead:

Func<object> lambda = new Func<Task<int>>(async () => { return await Task.FromResult(1); });
  • 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-14T00:05:27+00:00Added an answer on June 14, 2026 at 12:05 am

    The lambda is indeed returning Task<int>, but why does the compiler think that it should try to convert it to System.Func<object>?

    I believe the error message is poorly worded. It’s trying to derive a return type for the lambda (which can be void, Task, or Task<T>), and convert the lambda to Func<object>. I recommend that you raise an issue on Microsoft Connect requesting a clearer error message.

    On the other hand, if what the error message is trying to convey is that System.Func<Task<int>> cannot be assigned to System.Func<object>, that doesn’t seem to be the case thanks to generic delegate covariance

    True, but the compiler doesn’t determine the type of lambda expressions until “later” than most expressions. The compiler doesn’t see Func<Task<int>> (it just sees a lambda expression taking no arguments and returning Task or Task<int>), and so it won’t use generic delegate variance.

    I’m hoping that Eric Lippert will do a blog post on how async lambda expressions are resolved, particularly in the case of method overload selection.

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

Sidebar

Related Questions

I'm not 100% convinced that this is a good idea, but I bumped into
I'm trying to implement AES in c# and I bumped into an error. This
I've bumped into this issue while trying to install a MDM profile onto an
I have bumped into this problem several times on the type of input data
While answering another question I bumped into this interesting situation Where WCF is happy
I bumped into this problem when I created a chart that need a axis
I am trying to middle align an element, But I have bumped into this
I bumped into an additional question that I needed in regards to this: Using
I accidentally bumped into an ad like this: http://monotouch.net/DownloadTrial?ref=so1 . Then I start to
I bumped into this strange macro code in /usr/include/linux/kernel.h : /* Force a compilation

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.