MBZ
MBZ

Reputation: 27592

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?

Upvotes: 6

Views: 3402

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500335

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.

Upvotes: 9

carlosfigueira
carlosfigueira

Reputation: 87228

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.

Upvotes: 4

Related Questions