Initialize multiple variables in one line #1456
-
Currently if I do this:
Should be nice to allow the later so we don´t have to write 2 lines like:
|
Beta Was this translation helpful? Give feedback.
Replies: 18 comments
-
|
Beta Was this translation helpful? Give feedback.
-
Let me get this straight. Are you actually thinking of something like tihs: var (b1, b2) = true; where you're initializing both variables with the same value? If so, you can get pretty close with deconstructions: var (b1, b2) = (true, true); |
Beta Was this translation helpful? Give feedback.
-
A key problem with this is that adding another variable declaration might change the inferred type of all of them. Assume that this is legal:
with the compiler inferring Adding a third variable declaration like this:
would then change all three variables into Such a change would almost certainly break the rest of the method. If we're lucky, the result wouldn't compile. FWIW, every C# style guide I've ever read has put multiple declarations in one statement in the "Yeah, it's legal; but don't do it" category, along with thousand line methods, signatures with forty parameters, and inheritance twelve levels deep. |
Beta Was this translation helpful? Give feedback.
-
Yes I'm trying to do that, but this syntax is more clear I think: var b1, b2= true; Once you find an " = " you stop inferring, and assign the type after the = to b1, and b2. |
Beta Was this translation helpful? Give feedback.
-
If I may, public class C {
public void M() {
var (b1, b2) = true;
}
}
public static class Wheeeeeee {
public static void Deconstruct(this bool b, out bool b1, out bool b2) {
// Are you thinking what I'm thinking, b1?
// I think I am, b2.
// It's tuples time!
(b1, b2) = (b, b);
}
} On a more serious note, when you say that So when you say that |
Beta Was this translation helpful? Give feedback.
-
Yup, looks like @nanddonet didn't actually try to use |
Beta Was this translation helpful? Give feedback.
-
Then I think it's a little bit misleading. When I see
First thing I think is b1 and b2 it's gonna be initialized with true. |
Beta Was this translation helpful? Give feedback.
-
That would probably be why most style guides warn against it. |
Beta Was this translation helpful? Give feedback.
-
@nanddonet This example is just one case, totally equivalent to |
Beta Was this translation helpful? Give feedback.
-
Thought I misspelled your name when I mentioned you, but you've just renamed your account to @potudopotudo. (❔) |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h You might as well do it in general: static class Wheeeeeee
{
public static void Deconstruct<T>(this T b, out T b1, out T b2)
=> (b1, b2) = (b, b);
} ... or if you prefer class C
{
void M()
{
true.To(out var b1, out var b2);
}
}
static class Wheeeeeee
{
public static void To<T>(this T b, out T b1, out T b2)
=> (b1, b2) = (b, b);
} |
Beta Was this translation helpful? Give feedback.
-
I just added this concern, because it's not obvious, and misleading at for me. I think it's helpful what I propose, if people don't like the idea is fine. @theunrepentantgeek gave a good explanation and I expected the level of the answers to go in that direction, not to end up in jokes around extension methods. Anyway, thanks for the feedback.. |
Beta Was this translation helpful? Give feedback.
-
@potudopotudo Don't worry, he's referring to a long-running joke about someone else, not you! |
Beta Was this translation helpful? Give feedback.
-
What I hoped there would be is remove the limit of comma-separated multiple initialization of int x = 1, y = 2; We can do var x = 1, y = 2; I know I can use tuples, but it doesn't always make sense to combine the variables together. |
Beta Was this translation helpful? Give feedback.
-
@weitzhandler If it doesn't make sense to combine the declarations into one line, by all means don't do it! And especially don't ask for support for combining declarations in cases that, by your own admission, it doesn't make sense. |
Beta Was this translation helpful? Give feedback.
-
Actually you're right. I will explain why I talked about this in the first place, it's because I'm used to keep my lines as short as possible and split them into multiple lines when there are two or more things in it. int
myFirstVariable = GetFirstVariable(),
mySecondVariable = GetSecondVariable(),
myThirdVariable = GetThirdVariable(); Which can look a bit nasty when using tuples. For example: int
startingOffset = indicesArray.Count(index => index <= start);
endingOffset = indicesArray.Count(index => index <= end); |
Beta Was this translation helpful? Give feedback.
-
It's easier to manipulate the lines when they all start with |
Beta Was this translation helpful? Give feedback.
-
Ok you got me convinced. I'm wondering why this issue remains open then lol... |
Beta Was this translation helpful? Give feedback.
If I may,
On a more serious note, when you say that
bool b1, b2 = true
"works", it doesn't necessarily do what you might think it does -b1
is uninitialized,b2
istrue
.So when you say that
var b1, b2 = true;
should be allowed a shorthand forvar b1 = true; var b2 = true;
, neither of those are the same asbool b1, b2 = true;
.