csharp / beginner
Snippet
Asynchronous Task Execution
Uses the async and await keywords to pause execution without blocking the main thread, which is essential for responsive applications.
snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
using System;using System.Threading.Tasks;public class Program {public static async Task Main() {Console.WriteLine("Starting...");await Task.Delay(1000);Console.WriteLine("Finished after 1 second.");}}
Breakdown
1
async Task Main()
Declares an asynchronous entry point that returns a Task.
2
await Task.Delay(1000)
Asynchronously waits for 1000 milliseconds (1 second) without freezing the program.