Yesterday, I learned the basics about C# multitasking. While I technically seem to understand how it works, I just don't see why it's done like it is. Bearing in mind that I'm new on the topic, that's probably a sign that I have understood something wrong. For example, You could write:
public async Task SomeEventHandler()
{ if(foo) return await SomeMethod("a");
else return await SomeMethod("b");
}
async Task<int> SomeMethod(string whatever)
{ string bar = await SomeInput();
return bar + " " + whatever;
}
What I don't see here is that why are the async and await keywords required? Why can't one just write:
public void SomeEventHandler()
{ if(foo) return SomeMethod("a");
else return SomeMethod("b");
}
int SomeMethod(string whatever)
{ string bar = SomeInput();
return bar + " " + whatever;
}
With the input method implementation being something like:
public string SomeInput()
{ while(!InputObject.stuffIncoming)
{ CurrentTheard.DoSomethingElse();
}
return InputObject.Next();
}
Can somebody explain why that would not work, or would work in undesirable way? EDIT: According to answers it seems to be that for some reason, methods doing awaiting cannot be called like regular ones or vice-versa. But what causes that? At least in D you can both call or spawn a theard for a same function.