0

I have a function:

    public static async Task DoSomthing(string str1, int num1, bool bool1)
    {
        //Do something.... (This function will not return any value)
    }

I want to overload this function so it can be called with other arguments, but this function will basically call the function above.

Now I have 2 choices and I don't know what is the better one (in every aspect from performance to good coding)?

Option 1:

    public static Task DoSomething(CustomObject obj)
    {
        return DoSomthing(obj.str, obj.num1, obj.bool1);
    }

Option 2:

    public static async Task DoSomething(CustomObject obj)
    {
        await DoSomthing(obj.str, obj.num1, obj.bool1);
    }
Green Fire
  • 720
  • 1
  • 8
  • 21

2 Answers2

3

It will be executed almost in the same way but for the one with await it will add some overhead because the compiler will create a state machine.

Compiler creates state machine for methods with async-await keywords so when the control flow reaches await it is returned to the calling method. The execution of async method continues when the awaited Task (or method that returns Task) has finished execution.

As there is no code after await it is somewhat inadvisable to use await in this scenario. You could simply return a Task.

Fabjan
  • 13,506
  • 4
  • 25
  • 52
1

You can do option 1 because you only want to return the task witch it's passing.

Personally I prefer to read in the Taskname Async like DoSomthingAsync you you always know what's async and what's sync

public static Task DoSomethingAsync(CustomObject obj) 
    // or set the ConfigureAwait to true when you need stuff done on the same thread.
    => DoSomthingAsync(obj.str, obj.num1, obj.bool1).ConfigureAwait(false); 
StuiterSlurf
  • 2,464
  • 4
  • 34
  • 55