async Task vs async void in C#

Shervan360 1,681 Reputation points
2022-08-13T00:41:50.277+00:00

Hello,

I expected the compiler to show an error and tell me "return Task" from the Show method.

Why don't we need to return a Task? Task is a class and I expected to return an instance of Task class.

What is the difference between static async Task Show() and static async void Show() ?
When should we use each one?

(I am beginner to this topic, could you please write a sample example?)

namespace ConsoleApp2  
{  
    internal class Program  
    {  
        static void Main(string[] args)  
        {  
  
        }  
        static async Task Show() // is it equal with 'static async void Show()' ?  
        {  
             // In fact, **Task is a class and I expected to return an instance of Task class.**  
            Console.WriteLine("!");  
            // we don't return anything!  
        }  
    }  
}  
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,596 Reputation points Volunteer Moderator
    2022-08-13T06:38:05.197+00:00

    You should see siggles as shown below with text explaining what will happen and a recommendation.
    230846-ccc.png


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.