Challenge your understanding of asynchronous programming in C# using async and await, covering core concepts, common patterns, and potential pitfalls. Improve your grasp of modern concurrency techniques and effective management of asynchronous tasks in C#.
Which keyword must be added to a C# method declaration to enable the use of the await operator inside the method?
Explanation: The async keyword allows a method to use the await operator, enabling asynchronous operations within that method. Adding threaded or synchronous does not provide support for awaiting asynchronous operations. Awaiter is not a keyword in C#; it's a class related to the await pattern but not used in method declarations.
What is the most common return type for a C# async method that does not return a value?
Explanation: Task is the correct and recommended return type for async methods that do not return a value, as it conveys asynchronous work and allows monitoring of completion. Using void is reserved for event handlers in asynchronous code. Promise is not a C# type, and int would only be appropriate for a synchronous method returning an integer.
What does the await keyword do when used before a method returning Task in C#?
Explanation: Await pauses the execution of the method until the Task completes, resuming at the point where it was left off. It does not create a new thread; scheduling is managed by the runtime. Await does not convert code to be synchronous, nor does it cancel tasks automatically if they take too long.
How are exceptions thrown inside an awaited async method typically handled in C# if the Task is awaited?
Explanation: When an exception is thrown in an awaited async method, it is captured by the Task and re-thrown at the await statement. This enables standard try-catch exception handling. Ignoring exceptions or silent logging is incorrect, and async exceptions do not automatically terminate the application.
Which return type is recommended for asynchronous event handlers in C#?
Explanation: Asynchronous event handlers in C# should have a void return type to match the delegate signature for events. Using Task is not permitted for event handlers, while async is a modifier, not a return type. Func is a delegate type, not a method return type.
What happens if you call an async method without using await or capturing its returned Task?
Explanation: Calling an async method without await causes it to start asynchronously, but any exceptions may go unobserved and can lead to application crashes or unpredictable behavior. There is no compiler error, and the method does not execute synchronously, nor is it skipped entirely.
Which method produces a completed Task with a specified result and is commonly used in async methods for quick-return data?
Explanation: Task.FromResult quickly produces a completed Task with a given result and is often used for returning cached or computed data in async APIs. Task.Run schedules work asynchronously on a thread pool but does not return a completed task instantly. Task.Completed and Task.Await are not valid C# methods.
Which of the following statements about ConfigureAwait(false) is correct in C# async programming?
Explanation: ConfigureAwait(false) prevents resuming on the original synchronization context, which helps avoid deadlocks in certain scenarios. It does not retry or cancel tasks, nor does it force main thread execution. Using ConfigureAwait(false) is particularly important in library code.
How do you correctly declare an asynchronous lambda expression in C#?
Explanation: Declaring an asynchronous lambda requires prefixing it with async. Placing async after the lambda is invalid syntax. Simply using await in the body is not enough; the lambda must be declared async. Await cannot be used as a parameter name or type.
Starting with which version of C#, can you declare the Main method as async Task Main()?
Explanation: Support for async Task Main entry points was introduced in C# 7.1, allowing asynchronous code execution in the main method. Earlier versions, such as 5.0, 4.0, or 3.0, do not support this syntax, so using async in the Main method is not possible there.