.NET

Async/await & Patterns

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

26 면접 질문·
Senior
1

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

답변

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?

답변

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>?

답변

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 면접 질문

다음 면접을 위해 .NET을 마스터하세요

모든 질문, flashcards, 기술 테스트, 코드 리뷰 연습, 면접 시뮬레이터에 접근하세요.

무료로 시작하기