A. Missing await before File.WriteAllTextAsync call.
B. The method should return void instead of Task.
C. The file path "output.txt" is invalid.
D. Console.WriteLine cannot be used inside async methods.
Solution
Step 1: Check async call usage
The method calls File.WriteAllTextAsync but does not await it, so the write may not complete before printing.
Step 2: Understand await importance
Without await, the method does not wait for the file write to finish, which can cause unexpected behavior.
Final Answer:
Missing await before File.WriteAllTextAsync call. -> Option A
Quick Check:
Await async calls to ensure completion [OK]
Hint: Always await async file operations to complete them [OK]
Common Mistakes:
Forgetting await on async calls
Changing return type unnecessarily
Thinking Console.WriteLine is disallowed in async
5.
You want to read multiple text files asynchronously and combine their contents into one string. Which approach correctly uses async file reading to do this?
?
hard
A. Use await inside a loop to read each file one by one, appending results.
B. Read files using ReadAllTextAsync but ignore awaiting, just combine tasks directly.
C. Use synchronous ReadAllText inside an async method for simplicity.
D. Start all ReadAllTextAsync tasks without awaiting, then await them all together using Task.WhenAll and combine results.
Solution
Step 1: Understand async parallel reading
Starting all read tasks without awaiting lets them run in parallel, improving speed.
Step 2: Use Task.WhenAll to await all tasks
This waits for all reads to finish, then you can combine their string results safely.
Step 3: Avoid synchronous or missing await approaches
Reading synchronously blocks the thread; ignoring await causes incomplete results.
Final Answer:
Start all tasks, await with Task.WhenAll, then combine results. -> Option D