Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async get and set

is it possible to create properties with async get and set methods?

if yes, how?
if no, how should I call async methods in get and set properly?

like image 682
MBZ Avatar asked Sep 21 '12 16:09

MBZ


2 Answers

No. From section 10.15 of the C# 5 spec:

A method (§10.6) or anonymous function (§7.15) with the async modifier is called an async function. In general, the term async is used to describe any kind of function that has the async modifier.

So it's only methods, lambda expressions and anonymous methods that can use the async modifier.

Personally I'd think it somewhat odd to have a property like that anyway, especially as the property would have had to return Task<T> rather than T. A property should usually "feel" pretty lightweight - which doesn't really fit in with async.

like image 66
Jon Skeet Avatar answered Sep 28 '22 22:09

Jon Skeet


No. You can create methods which look like properties (async Task<T> getFoo() and async Task setFoo(T item)), but they're not properties per se.

like image 31
carlosfigueira Avatar answered Sep 28 '22 22:09

carlosfigueira