Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are increment operators possible on properties while ref is not

Tags:

c#

properties

ref

Say you have a class like this

public class Foo
{
   public int Bar { get; set; } = 42;
}

If you try to pass the property as a ref parameter the compiler issues the error

CS0206 A property or indexer may not be passed as an out or ref parameter

This is understandable since in practice the property in the above example is compiled into get_Bar() and set_Bar() methods. But if you use an increment operator on the property, like

var foo = new Foo();
foo.Bar++;

it works as expected. To achieve this, the compiler needs to produce something like this pseudo code:

var foo = new Foo();
int tmp = foo.get_Bar();
tmp++;
foo.set_Bar(tmp);

So in theory the compiler could do a similar thing for ref like:

var foo = new Foo();
int tmp = foo.get_Bar();
DoSomething(ref tmp);
foo.set_Bar(tmp);

Is there a technical reason why the compiler doesn't do that or was this just a design decision of the C# team?

like image 494
Bill Tür stands with Ukraine Avatar asked Nov 27 '16 11:11

Bill Tür stands with Ukraine


People also ask

Why increment and decrement operators can not be applied to final variables?

The increment and decrement operators can not be applied to final variables because of the simple reason that their value can not be changed. We can apply ++ and — operators for all primitive data types except Boolean type as it only has true and false which even sounds impractical.

What is the difference between the pre-increment and post-decrement operators?

The pre -increment and pre -decrement operators increment (or decrement) their operand by 1, and the value of the expression is the resulting incremented (or decremented) value. The post -increment and post -decrement operators increase (or decrease) the value of their operand by 1,...

What is the use of increment and decrease operator in JavaScript?

Increment operator is used to incrementing a value by 1. There are two varieties of increment operator: Post-Increment: Value is first used for computing the result and then incremented. Pre-Increment: Value is incremented first and then the result is computed. Decrement operator is used for decrementing the value by 1.

What is the use of increment and decrement in C?

Increment and Decrement Operators in C. C has two special unary operators called increment (++) and decrement (--) operators. These operators increment and decrement value of a variable by 1. Increment and decrement operators can be used only with variables. They can’t be used with constants or expressions.


1 Answers

Like HansPassant said, this was a design decision that the C# team made when writing the C# specification, so you would have to ask one of them in order to get a proper answer.

If I was to hazard a guess, though, it would be that the amount of compiler magic to get passing a property with ref would cause enough unobvious operations to happen behind the scenes as to make the solution undesirable. For example, how incrementing/decrementing a property works currently is as you say: the program assigns the value of the property's backing field to a temporary variable, performs the operation, and reassigns the result to the property. It's a straightforward process that doesn't incorporate any difficult concepts.

To do the same behind the scenes magic to pass a property with ref, however, the process becomes a bit more involved. When a value type is passed by ref, the actual value getting passed through the parameter is a pointer to the value type variable. To do this for a property, though, you would have to do something akin to your second example. That would result in the address of the temporary variable, not the property itself, to be passed to the method. That kind of behavior could cause some unforeseen and difficult to understand consequences for someone trying to manipulate the ref parameter in certain ways.

So there's my guess, that the increment operator is simple to wrap because it only deals with values, whereas the ref keyword is more complicated because it has to worry about scopes and memory addresses as well.

EDIT: Another reason that occurred to me, for a field, any manipulations inside the called method would be reflected on the field itself. These manipulations could be seen by other threads and such that accessed that field during the method's execution (best practices about concurrent field accessibility aside).

For a parameter, however, any changes that happen inside the method wouldn't be visible until the method returned and the value copied back over. This would lead to inconsistent behavior between fields and properties, the cause of which wouldn't be readily apparent.

(Personally, I think this is a more probable reason to not support ref on properties.)

like image 63
Abion47 Avatar answered Oct 02 '22 17:10

Abion47