.NET

Async/await & Patterns

async/await, Task, ValueTask, ConfigureAwait, cancellation tokens, parallel execution, deadlocks

26 câu hỏi phỏng vấn·
Senior
1

What is the main role of the 'await' keyword in an asynchronous method?

Câu trả lời

The await keyword suspends the execution of the asynchronous method until the Task completes, while freeing the calling thread to process other tasks. This avoids thread blocking and improves application scalability. Unlike Task.Wait() or Task.Result which block the thread, await enables efficient use of thread pool resources.

2

Why should async void be avoided except for event handlers?

Câu trả lời

Async void methods cannot be awaited and exceptions thrown inside cannot be caught by calling code, causing application crashes. Additionally, unit testing async void methods is impossible as there is no Task to await. Event handlers are the exception because they are called by the framework and cannot return Task.

3

In which use case should ValueTask<T> be preferred over Task<T>?

Câu trả lời

ValueTask is a value type that avoids heap allocation when the result is already available synchronously, such as in a cache. This reduces garbage collector pressure in high-performance scenarios. Task is always heap-allocated even if the result is synchronous. Use ValueTask only when performance gains are measurable, as incorrect usage can degrade performance.

4

What important restriction applies to ValueTask<T> compared to Task<T>?

5

What is the main purpose of ConfigureAwait(false) in a .NET library?

+23 câu hỏi phỏng vấn

Nắm vững .NET cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí