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
"task
or derived type" doesn't mean it'stask
ortask<t>
. expect if calledmymethodasync<mycustomtask>
mycustomtask
derivestask
?the compiler needs know whether it's building state machine returning
task
ortask<t>
when compiles method - uses different helper classes in different casesif async method has return type of
task
,return
statements can't specify value; if has return type oftask<t>
return
statements must specify value implicitly convertiblet
. how can work withinmymethodasync
? it's bit saying "my method eithervoid
or returnst
- can decide when call it".
it's not clear you're trying achieve here, isn't going work.
Comments
Post a Comment