I have some C# code (in a .NET Standard 2.0 library) that looks like the following:
private object MySynchronousOp(object param, object cmnParam)
{
//Do stuff...
return theObject;
}
private ResponseT Do<ResponseT, RequestT>(Func<RequestT, object, ResponseT> op, RequestT request)
{
//Do some checks, get/setup cmnParam.
return op.Invoke(request, cmnPatam);
}
public object RunMySynchronousOp(object param)
{
return Do(MySynchronousOp, param);
}
This pattern is repeated for many MySynchronousOp and RunMySynchronousOp pairs, using Do to do a common set of checks and provide a valid common parameter.
Now I am adding an asynchronous pair of methods like the following, without changing the Do() method already defined:
private async Task<object> MyAsynchronousOp(object param, object cmnParam)
{
//Do stuff, with awaits here...
return theObject;
}
public async Task<object> RunMyAsynchronousOp(object param)
{
return Do(MyAsynchronousOp, param);
}
This compiles, which surprised me and sitting here I find suspicious. I am not yet in a position to actually test this change, but was curious if I should expect this to work without any nasty surprises or unexpected behaviors. I am used to thinking of async methods as colored functions needing to be either called by other async functions or have Wait() called on the Task returned from it to make it synchronous again, but this would be a case where the async nature of the My... method passed through the Do method and showed up again in the Run... method, which I don't know if I believe.
Am I missing something?
Edit: As a note, I've used object in my example code here, but the actual code uses other types. I didn't copy and paste from the actual code because of work-related reasons. In the actual code there are several MySynchronousOp and RunMySynchronousOp methods each with different RequestT and ResponseT types. I just didn't want to replicate or invent any of that here and as far as I know (though I could be wrong I suppose) it doesn't affect the meat of my question.