Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting multiple variables in one line

Tags:

c#

In perl I can define a bunch of variables in 1 line:

my value=0;
my (a,b,c)=value;

is there some similar C# way of doing the same? here is what I have:

const bool free = true;
bool t1,t2,t3;

private void Form1_Load(object sender, EventArgs e) {
    //t1,t2,t3=free;
}
like image 687
user2864613 Avatar asked Oct 22 '25 08:10

user2864613


2 Answers

I believe you can do t1 = t2 = t3 = free; However, it really makes it much harder to read. I would not recommend it.

like image 55
Jonathan Nixon Avatar answered Oct 24 '25 21:10

Jonathan Nixon


C# does not support full destructuring assignment. Thus, there is no C# equivalent of

my @v = (1,3,5);
my ($a,$b,$c) = @v;

However, as pointed out by @Guthwulf, in the case of a scalar you can write t1 = t2 = t3 = free and it will assign the same value to each element.

like image 44
SheetJS Avatar answered Oct 24 '25 23:10

SheetJS