Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Async Task Method Without Await or Return

I have an Interface I that is implemented in two places like:

interface I 
{ 
   Task DoSomething();
}

The interface has async Task DoSomething method API that is then implemented in class A like:

class A : I {....}

class B : I {....}

In class A, the implementation of DoSomething is like below and that is OK:

public async Task DoSomething()
{
    if (...)
    {
        await DoIt();
    }
}

However, in class B, the implementation of DoSomething() should not do anything. So, its implementation looks like this:

public async Task DoSomething()
{
    // nothing
}

This compiles but I am not sure how correct it is do do something like this beside the fact that the method is useless.

But being "useless" in this case is ok in this case since it is implemented just because it is required by class B implementing the interface I.

I wonder if this is a correct way to implement a method that returns async Task but has no await or return? I know that this method will simple do nothing and execute synchronously as there is no call to await.

UPDATE: Similar questions have been asked here on SO and I have checked all of them before asking this one. None is asking what I am asking though

like image 910
pixel Avatar asked Nov 08 '17 18:11

pixel


People also ask

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

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();
}
like image 136
sellotape Avatar answered Sep 20 '22 23:09

sellotape


I see that most people prefer to leave out the async and use Task.ComletedTask instead. But even if await is not used, there is still a big difference in exception handling

Consider the following example

static async Task Main(string[] args)
{

    Task task = test(); // Will throw exception here
    await task;

    Task taskAsync = testWithAsync();
    await taskAsync; // Will throw exception here
}

static Task test()
{
    throw new Exception();
    return Task.CompletedTask; //Unreachable, but left in for the example
}

static async Task testWithAsync()
{
    throw new Exception();
}

Using

test().ContinueWith(...); or Task.WhenAll(test())

may result in unexpected behaviour.

Therefore, I prefer async instead of Task.CompletedTask or Task.FromResult.

like image 39
Jan-Fokke Avatar answered Sep 20 '22 23:09

Jan-Fokke