Task, async and await, ConfigureAwait, cancellation tokens, Parallel, locks and thread safety in C#.
40 cards · basic cards · AI-written, checked twice. Edit anything.
- What is an async method in C#?
Show answer
A method that can pause execution and resume later without blocking threads, marked with the async keyword.
- What does the await keyword do?
Show answer
Pauses execution of an async method until the awaited Task completes, then resumes automatically.
- What is the difference between Task and Task<T>?
Show answer
Task represents an operation with no return value. Task<T> represents an operation that returns a value of type T.
- What does ConfigureAwait(false) do?
Show answer
Prevents the continuation from running on the original synchronization context after the await.
- When should you use ConfigureAwait(false)?
Show answer
In library code where you do not need to return to the UI or original synchronization context.
- What is a cancellation token in C#?
Show answer
A mechanism to signal that an operation should stop, preventing unwanted work from running.
- What class provides cancellation tokens?
Show answer
CancellationTokenSource creates tokens. CancellationToken is the parameter type passed to operations.
- How do you check if cancellation was requested?
Show answer
Call ThrowIfCancellationRequested() to throw an exception, or check the IsCancellationRequested property.
- What does Parallel.For do?
Show answer
Executes loop iterations concurrently across multiple threads instead of sequentially.
- What is the difference between Parallel.For and a regular for loop?
Show answer
Parallel.For may execute iterations out of order and concurrently. A regular for loop is strictly sequential.
- What is PLINQ?
Show answer
Parallel LINQ. Adding .AsParallel() to a LINQ query makes it execute across multiple cores.
- What is the lock keyword used for?
Show answer
Acquires an exclusive lock on an object to ensure only one thread accesses code at a time.
- What happens if a thread tries to acquire a lock that is already held?
Show answer
The thread blocks until the lock is released by the thread holding it.
- What is Monitor.Enter and Monitor.Exit?
Show answer
Low-level synchronization methods: Monitor.Enter acquires a lock on an object, Monitor.Exit releases it.
- What is a Mutex in C#?
Show answer
A kernel-level synchronization primitive that ensures only one thread can access a resource at a time. Can be named and cross-process.