Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initialize multipile variables with different values (same as javascript) [duplicate]

Tags:

variables

c#

i was searching if there's a way to initialize multiple variables with different values on the same line, same as you can do in JavaScript. (maybe in c# 6 ?)

for example:

var x = "hello", 
    y = "friend";

** EDIT

I know i can assign with the same value (seen other posts on SO as well - i don't see how this post is duplicated ) i would like it to be with different values (using the var key word). as i see from the answers below i see there's no way without explicitly declare the type. so thanks for the help.

like image 988
Sagiv b.g Avatar asked Mar 22 '26 01:03

Sagiv b.g


2 Answers

just assign like that:

string str = "1", str1="2", str3="3", str4="4", str5="5";

please, remember, that implicitly-typed local variables cannot have multiple declarations so this code will not be compiled:

var someVar1 = "1", someVar2 = "2", someVar3 = "3", someVar4 = "4", someVar5 = "5";//This line is error and is not compiled!
like image 153
StepUp Avatar answered Mar 24 '26 14:03

StepUp


Implicitly-typed variables cannot have multiple declarators. You have to specify the type.

string x = "hello", 
       y = "friend";
like image 41
vincentp Avatar answered Mar 24 '26 13:03

vincentp