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 8759651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:51:35+00:00 2026-06-13T14:51:35+00:00

We have this method: async Task<int> AccessTheWebAsync() { HttpClient client = new HttpClient(); Task<string>

  • 0

We have this method:

async Task<int> AccessTheWebAsync()
{ 
    HttpClient client = new HttpClient();

   Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");

   // You can do work here that doesn't rely on the string from GetStringAsync.
   DoIndependentWork();

   string urlContents = await getStringTask;
   //The thing is that this returns an int to a method that has a return type of Task<int>
   return urlContents.Length;
}

Does an implicit conversion occur between Task<int> and int? If not, then what is happening? How is it implemented to work?

  • 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-13T14:51:37+00:00Added an answer on June 13, 2026 at 2:51 pm

    Does an implicit conversion occur between Task<> and int?

    Nope. This is just part of how async/await works.

    Any method declared as async has to have a return type of:

    • void (avoid if possible)
    • Task (no result beyond notification of completion/failure)
    • Task<T> (for a logical result of type T in an async manner)

    The compiler does all the appropriate wrapping. The point is that you’re asynchronously returning urlContents.Length – you can’t make the method just return int, as the actual method will return when it hits the first await expression which hasn’t already completed. So instead, it returns a Task<int> which will complete when the async method itself completes.

    Note that await does the opposite – it unwraps a Task<T> to a T value, which is how this line works:

    string urlContents = await getStringTask;
    

    … but of course it unwraps it asynchronously, whereas just using Result would block until the task had completed. (await can unwrap other types which implement the awaitable pattern, but Task<T> is the one you’re likely to use most often.)

    This dual wrapping/unwrapping is what allows async to be so composable. For example, I could write another async method which calls yours and doubles the result:

    public async Task<int> AccessTheWebAndDoubleAsync()
    {
        var task = AccessTheWebAsync();
        int result = await task;
        return result * 2;
    }
    

    (Or simply return await AccessTheWebAsync() * 2; of course.)

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

Sidebar

Related Questions

I have this method to display the contact numbers in my inbox: public ArrayList<String>
I have this method for grabbing the file name from a string URI. What
I have this method to transfer files using a FTP Server: private void TransferNeededFiles(IEnumerable<string>
I have this method in cs page: public String getToolTip(Object productId, Object imgBtnId) {
I have created an asynchronous task like this: private class LongOperationcheckall extends AsyncTask<String, String,
I have a problem with async-await expression which returns wrong result. private Task<int> A
I have a NetworkStream which I read asynchronously (using async/await) await Task<int>.Factory.FromAsync((cb, state) =>
I have this method which lets me translate the position of an object, animating
I have this method which leaks ~ 6KB : + (EInspectorFacilityInfo*) newWithNode: (CXMLNode*) node
I have this method (someone else wrote it!) - (CGPDFDocumentRef)getPdf { NSArray *paths =

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.