C# Async Task Method Without Await or Return


 public Task DoSomething()
{
    return Task.CompletedTask;
}
No need for the async.
If you're using an older version of .NET, use this:
public Task DoSomething()
{
    return Task.FromResult(0);
}
If you find you need to return a result but you still dont need to await anything, try;
public Task<Result> DoSomething()
{
    return Task.FromResult(new Result())
}
or, if you really want to use async (not recommended);
public async Task<Result> DoSomething()
{
    return new Result();
}

Post a Comment

1 Comments

  1. Share great information about your blog , Blog really helpful for us .
    Reference: https://v8web.com/

    ReplyDelete