Avatar ·

How and when to correctly use async and await in C#? Let's figure it out with examples

Hello everyone! I'm a beginner C# developer, and I just can't figure out async/await. I've read many articles, but in practice I still get confused.\n\nHere's a simple example. I have a method that loads data from a web server and processes it. I've heard that for long operations (like downloading a file or a database query) you need to use async so as not to block the UI. But I wrote this code, and it still freezes:\n\n```csharp\npublic async Task LoadDataAsync()\n{\n using (HttpClient client = new HttpClient())\n {\n // I thought await would do the magic\n var response = await client.GetStringAsync("https://example.com/data");\n // But the UI still freezes during loading!\n return ProcessData(response);\n }\n}\n\nprivate void Button_Click(obj