Ternary/null coalescing operator and assignment expression on the right-hand side?

房东的猫 提交于 2019-12-04 10:37:46

This happens as consequence of the assignment operator also returning the value:

The assignment operator (=) stores the value of its right-hand operand in the storage location, property, or indexer denoted by its left-hand operand and returns the value as its result.

The expression b = 12 not only assigns 12 to b, but also returns this value.

Multiple assignment works in C#:

int a;
int b;
int c;
a = b = c = 5;
//all variables = 5;

if(6 == (c = 6)){
   //this will be true;
}

If you put a variable on the right side of an equation, even if it has just been assigned a value itself on the same line, it returns its value/reference.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!