.NET

Async/await & Patterns

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

26 preguntas de entrevista·
Senior
1

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

Respuesta

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?

Respuesta

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

Respuesta

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 preguntas de entrevista

Domina .NET para tu próxima entrevista

Accede a todas las preguntas, flashcards, tests técnicos, ejercicios de code review y simuladores de entrevista.

Empieza gratis