Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple inline assignments in one statement in c#

Obviously the following is totally fine in c#;

int a;
int b = a = 2;

Is it possible to do multiple variable assignments in c# in a single statement?

i.e. something like;

int a = (int b = 2);
like image 857
maxp Avatar asked Mar 26 '26 16:03

maxp


2 Answers

If we look at:

int a;
int b = a = 2;

That is essentially a=2; then b=a; (but without an extra eval). So we can get similar by reversing the order:

int a = 2, b = a;

However: I would say take this a bit hesitantly: please also prioritise readability.

like image 143
Marc Gravell Avatar answered Mar 28 '26 05:03

Marc Gravell


Not as far as I know. The only variation I know of is:

int a = 2, b = 2;
like image 25
Kristian Fenn Avatar answered Mar 28 '26 05:03

Kristian Fenn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!