Consider this C# code that runs synchronously. What will it print?
using System; using System.Threading; class Program { static void Main() { Console.WriteLine("Start"); Thread.Sleep(2000); Console.WriteLine("End"); } }
Thread.Sleep pauses the program for 2 seconds before continuing.
The program prints "Start", then waits 2 seconds because of Thread.Sleep, then prints "End".
Look at this C# async code. What will it print?
using System; using System.Threading.Tasks; class Program { static async Task Main() { Console.WriteLine("Start"); await Task.Delay(2000); Console.WriteLine("End"); } }
Task.Delay asynchronously waits without blocking the thread.
The program prints "Start", then asynchronously waits 2 seconds, then prints "End".
Why do UI applications use async programming?
Think about what happens if the UI freezes while waiting.
Async programming lets the UI stay active and responsive while waiting for long tasks, improving user experience.
What is the output or behavior of this code?
using System; using System.Threading.Tasks; class Program { static void Main() { Console.WriteLine("Start"); var task = Task.Delay(1000); task.Wait(); Console.WriteLine("End"); } }
Task.Wait blocks the current thread until the task finishes.
The program prints "Start", blocks for 1 second waiting for Task.Delay, then prints "End".
Why do server applications use async programming?
Think about how servers handle many users at once.
Async programming allows servers to serve many users by not blocking threads during waits, improving scalability.