0

Is there any way to initialize these two variable at the same time?

in this example "time" variable can be "" or has a value.

var variable1 = string.IsNullOrEmpty(time) ? string.Empty : "value";
var variable2 = string.IsNullOrEmpty(time) ? "value" : string.Empty;
Mina
  • 377
  • 1
  • 4
  • 11
  • 5
    Perhaps you can explain why you need to do this? – DavidG Nov 19 '14 at 13:03
  • well, when one gets one value, the other gets the other value and vice versa and the condition is the same. so I thought there could be a way to make it shorter. – Mina Nov 19 '14 at 13:06
  • 1
    Shorter often means less readable. I wouldn't be concerned about shortening 2 lines of code. – DavidG Nov 19 '14 at 13:07
  • @DavidG Unless there is a large penalty for evaluating the condition. (Yeah .. I know .. premature optimization) – Peter M Nov 19 '14 at 13:08
  • If you're concerned about checking the condition twice, you can use simple if statement. – Sriram Sakthivel Nov 19 '14 at 13:09
  • @PeterM Absolutely, but in that case I'd probably make it longer and use an `if` statement (as suggested by Sriram) – DavidG Nov 19 '14 at 13:10
  • @DavidG I probably would too, but I can understand where Mina is coming from. – Peter M Nov 19 '14 at 13:11

4 Answers4

1

Here you go, a completely unreadable mess:

string time = null;

string variable1, variable2 = (variable1 = string.IsNullOrEmpty(time) ? string.Empty : "value") == string.Empty ? "value" : string.Empty;

In case you couldn't tell, this isn't a serious suggestion.

RobH
  • 3,676
  • 1
  • 21
  • 41
-1

Not possible. But you can create some helper class to hold those two variables. Or you can use some out-of-the-box, like Tuple:

var variable = string.IsNullOrEmpty(time) ? Tuple.Create(string.Empty, "value") 
                                          : Tuple.Create("value", string.Empty);

and then access those two values as variable.Item1 and variable.Item2.

Note: Use it wisely as variables are in general better because they have names, and hence - some meaning. Too many Tuples with all those Item1 and Item2 can fast become unclear, what they are intended for.

Konrad Kokosa
  • 15,790
  • 2
  • 33
  • 54
-1

No. Assuming "value" is the same in both cases the most you could do is replace "value" with variable1 in the second line.

Alan B
  • 3,823
  • 19
  • 33
-2

It is possible to initialize multiple variables on a single line of code to the same value:

    string variable2;
    var variable1 = variable2 =(string.IsNullOrEmpty(time) ? string.Empty : "value");

However, I think this is unreadable and would avoid it.

With what you are trying to do (from your comment), I would use a simple if statement so that the IsNullOrEmpty check is only executed once.

    string variable1;
    string variable2;
    if (string.IsNullOrEmpty(time))
    {
        variable1 = null;
        variable2 = "value";
    }
    else
    {
        variable1 = "value";
        variable2 = null;
    }
Allan Elder
  • 3,676
  • 14
  • 19