none
What's the difference between async Task<T> method and Task<T> method.

    Question

  • when I coding the method, I confused about the two fashion mentioned by title

    first, both of them is awaitable;

    second, Both of them is typof(Task);

    seems like the Task<T> method delay the async point.

    when I code the base class, I can't decide to implement which fashion.

    please help me, thank you.

    Friday, April 21, 2017 8:23 AM

Answers

  • If you don't put async keyword in the method signatures which returns Task  or Task<T>, you wouldn't be able to use await keyword inside that method body, but the caller of the method can await it as it is returning a Task. I would recommend to read the following article for more details:

    Async and Await by Stephen Cleary

    Hope It Helps!



    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by CrazyOldPotato Wednesday, April 26, 2017 1:22 AM
    Friday, April 21, 2017 10:01 AM

All replies

  • Hi!

    The only difference is that in the one method you can await, in the other not. Also, in the async method, the compiler will make sure that you return a Task (or Task<T>) and does this implicit for you. That's all.

    It was done like this (having to declare a method async) because otherwise it could break a lot of code with the introduction of the await keyword.

    Hope it wil be useful.

    If it's good for you, can you mark this answer as answer of your question please?

    Follow me on twitter : https://twitter.com/NordineMhoumadi


    Download my NEW FREE application MCP PREPARATION in the Windows 10 Store for preparing your Microsoft Certification or test your skills in Microsoft Technologies;

    • Proposed as answer by Chad333 Friday, April 21, 2017 4:19 PM
    Friday, April 21, 2017 8:29 AM
  • If you don't put async keyword in the method signatures which returns Task  or Task<T>, you wouldn't be able to use await keyword inside that method body, but the caller of the method can await it as it is returning a Task. I would recommend to read the following article for more details:

    Async and Await by Stephen Cleary

    Hope It Helps!



    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by CrazyOldPotato Wednesday, April 26, 2017 1:22 AM
    Friday, April 21, 2017 10:01 AM
  • Hi CrazyOldPotato,

    If you search in the forum you will get that this same question was asked many times. You can look into those answers also. Hope this helps you.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Friday, April 21, 2017 10:49 AM
    Moderator
  • You could also look into the below article it has explains step by step. Hope this helps you.

    Task.Run vs Async Await


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Friday, April 21, 2017 11:50 AM
    Moderator