c# - Using a generic type as a return type of an async method -
a previous question made me wonder why following method raise compile time error:
the return type of async method must void, task or task
public async t mymethodasync<t>() t : task { // irrelevant code here returns task } since know @ compile time t task or derived type, why won't work?
edit
the reason i'm asking method may return task or task<t>. let's method can return either , don't want duplicate code.
of course theoretical , isn't ment production purposes.
edit 2
found great article lucian wischik: why must async return task
three problems:
just because
t"taskor derived type" doesn't mean it'staskortask<t>. expect if calledmymethodasync<mycustomtask>mycustomtaskderivestask?the compiler needs know whether it's building state machine returning
taskortask<t>when compiles method - uses different helper classes in different casesif async method has return type of
task,returnstatements can't specify value; if has return type oftask<t>returnstatements must specify value implicitly convertiblet. how can work withinmymethodasync? it's bit saying "my method eithervoidor returnst- can decide when call it".
it's not clear you're trying achieve here, isn't going work.
Comments
Post a Comment