async Task vs async void in C#

Shervan360 1,481 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!  
        }  
    }  
}  
C#
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.
10,302 questions
{count} votes

Accepted answer
  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,196 Reputation points
    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