Preamble code for properties #7981
Replies: 3 comments 3 replies
-
I don't see much use for such a feature outside of guard clauses, and I don't believe that properties should have behaviors (e.g. they shouldn't throw exceptions). Can you give an example that isn't a guard clause that would more thoroughly demonstrate the usefulness of such a feature? |
Beta Was this translation helpful? Give feedback.
-
Probably because nobody has proposed for such code to have a meaning in the language. The property itself is not a method, so one would have to define what it means to allow for statements to appear there outside of the accessor methods. Allowing for arbitrary statements to exist within the property would probably conflict with property-scoped fields, which is a championed proposal. |
Beta Was this translation helpful? Give feedback.
-
Not really, the use case is a guard, that's the impetus for the concept. I agree too that ordinarily properties should not throw, but if the index is invalid then throwing (in this admittedly limited case) is appropriate. I'm creating lots of similar structures and so this is coming up as I write this stuff, I accept this is a narrow case but it can get unsightly adding the guard into every get/set when the code is otherwise very simple. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Occasionally I find myself wondering why we can't write stuff like this:
Have some common code that runs irrespective of whether its a
get
or aset
. Of course this is a rare case because this is a rare thing, a property that accepts an argument, but it would be neat if we could do it, some indexers also take multiple arguments as well.Beta Was this translation helpful? Give feedback.
All reactions