0
0
CsharpHow-ToBeginner · 3 min read

How to Create Async Method in C# - Simple Guide

To create an async method in C#, use the async keyword before the return type and return a Task or Task<T>. Inside the method, use await to call asynchronous operations without blocking the thread.
📐

Syntax

An async method in C# is declared with the async keyword before the return type. It usually returns Task for methods without a result or Task<T> for methods returning a value. Inside the method, you use await to pause execution until the awaited task completes.

  • async: marks the method as asynchronous
  • Task: represents an ongoing operation without a return value
  • Task<T>: represents an ongoing operation that returns a value of type T
  • await: waits for the asynchronous operation to finish without blocking
csharp
public async Task MethodNameAsync()
{
    await SomeAsyncOperation();
}

public async Task<int> MethodNameAsync()
{
    int result = await SomeAsyncOperationReturningInt();
    return result;
}
💻

Example

This example shows an async method that waits for a delay and then returns a message. It demonstrates how to declare, call, and await an async method.

csharp
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        string message = await GetMessageAsync();
        Console.WriteLine(message);
    }

    static async Task<string> GetMessageAsync()
    {
        await Task.Delay(1000); // Simulate async work
        return "Hello from async method!";
    }
}
Output
Hello from async method!
⚠️

Common Pitfalls

Common mistakes when creating async methods include:

  • Not using await inside an async method, which can cause the method to run synchronously.
  • Returning void instead of Task or Task<T>, which makes error handling difficult.
  • Blocking on async code using .Result or .Wait(), causing deadlocks.
csharp
/* Wrong way: async method returning void and no await */
public async void WrongAsyncMethod()
{
    // No await here, runs synchronously
    Console.WriteLine("Running");
}

/* Right way: async method returning Task and using await */
public async Task RightAsyncMethod()
{
    await Task.Delay(500);
    Console.WriteLine("Running asynchronously");
}
📊

Quick Reference

Remember these key points when creating async methods in C#:

  • Use async keyword before the return type.
  • Return Task or Task<T>, not void, unless for event handlers.
  • Use await to call asynchronous operations inside the method.
  • Async methods help keep your app responsive by not blocking threads.

Key Takeaways

Use the async keyword and return Task or Task to create async methods.
Always await asynchronous operations inside async methods to avoid blocking.
Avoid async methods returning void except for event handlers to enable error handling.
Do not block async code with .Result or .Wait() to prevent deadlocks.
Async methods improve responsiveness by running operations without blocking the main thread.