A
Alexander Arlievsky
Hi,
C# compiler issues error message on the following lines:
GetStruct().Depth = 10;
if GetStruct() returns structure (value type). It seems to be correct - else
you will modify temporary value on the stack.
It still allows to call method, so GetStruct().SetDepth(10) compiles
perfectly, although obviosly still modifies only temporary value on the
stack.
But in our case "Depth" is property, and its "set" implementation modifies
some external object - our structure is just wrapper around
reference to other object. We made this wrapper value type to prevent
excessive memory allocations for each GetStruct() call.
So in our case it makes sense to write GetStruct().SetDepth(10) - it
modifies non-temporary object,
and if you write on the next line Assert(GetStruct().Depth == 10) -
assertion isn't failed.
It looks like compiler should threat property call here as method call, not
as simple field assignment.
Any ideas ?
Alexander Arlievsky
C.T.O. mPrest Ltd.
(e-mail address removed)
C# compiler issues error message on the following lines:
GetStruct().Depth = 10;
if GetStruct() returns structure (value type). It seems to be correct - else
you will modify temporary value on the stack.
It still allows to call method, so GetStruct().SetDepth(10) compiles
perfectly, although obviosly still modifies only temporary value on the
stack.
But in our case "Depth" is property, and its "set" implementation modifies
some external object - our structure is just wrapper around
reference to other object. We made this wrapper value type to prevent
excessive memory allocations for each GetStruct() call.
So in our case it makes sense to write GetStruct().SetDepth(10) - it
modifies non-temporary object,
and if you write on the next line Assert(GetStruct().Depth == 10) -
assertion isn't failed.
It looks like compiler should threat property call here as method call, not
as simple field assignment.
Any ideas ?
Alexander Arlievsky
C.T.O. mPrest Ltd.
(e-mail address removed)