Why does the assignment operator return the assigned value in C #?

The assignment operator in C # returns the assigned value. It is not clear where / how this function can be useful. Using it in an odd syntax like this, you can save a line of code, but will not be useful to read:

private String value; public void SetAndPrintValue(String value) PrintValue(this.value = value); } private static void PrintValue(String value) { /* blah */ } 

What is his purpose?

+7
c #
source share
2 answers

The assignment is a staple of many languages ​​returning to C (and possibly earlier). C # supports it because it is a common function of such languages ​​and has limited use of & mdash, like the goto operator.

Sometimes you can see this code:

 int a, b, c; for(a = b = c = 100; a <= b; c--) { // some weird for-loop here } 

Or that:

 var node = leaf; while(null != node = node.parent) node.DoStuff(); 

This may make some code a little more compact or allow you to do some tricky tricks, but that of course does not make it more readable. I would recommend against this in most cases.

+12
source share

I usually use it to assign the same properties to a control.

 btnSubmit.Enabled = btnAdd.Enabled = btnCancel.Enabled = txtID.Enabled= false; 
+4
source share

All Articles