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's task or task<t>. expect if called mymethodasync<mycustomtask> mycustomtask derives task?

  • the compiler needs know whether it's building state machine returning task or task<t> when compiles method - uses different helper classes in different cases

  • if async method has return type of task, return statements can't specify value; if has return type of task<t> return statements must specify value implicitly convertible t. how can work within mymethodasync? it's bit saying "my method either void or returns t - can decide when call it".

it's not clear you're trying achieve here, isn't going work.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -