13

How could you do the following inline conditional for a string[] array in C#. Based on a parameter, I'd like to include a set of strings...or not. This question is a followup of this one on stackoverflow.

        //Does not compile
        bool msettingvalue=false;
        string[] settings;
        if(msettingvalue)
            settings = new string[]{
                "setting1","1",
                "setting2","apple",
                ((msettingvalue==true) ? "msetting","true" :)};

If msettingvalue is true, I'd like to include two strings "msetting","true" : otherwise no strings.

Edit1 It doesn't have to be a key value pair...what if it were 5 strings to be (or not to be) added...I didn't think it'd be that tricky.

(Also...could someone with enough rep make a "inline-conditional" or "conditional-inline" tag?)

3
  • 2
    Based on your code sample, msettingvalue will always be true inside that array initialisation statement. Commented Oct 17, 2011 at 0:59
  • 1
    FWIW, perhaps a Dictionary or KeyValuePair[] (or ...) is better suited for setting pairs? Commented Oct 17, 2011 at 1:06
  • 2
    Use a dictionary, you're coding yourself into a corner here where there is no reason to do this at all. Commented Oct 17, 2011 at 1:54

2 Answers 2

22
settings = new string[]{"setting1","1", "setting2","apple"}
    .Concat(msettingvalue ? new string[] {"msetting","true"} : new string[0]);
    .ToArray()
Sign up to request clarification or add additional context in comments.

1 Comment

@pst it is only as ugly as given requirement. If I were to define it I would definitely NOT use a string array to store key/value pairs. Probably would be a Dictionary<key,value>, in which case you can get away with setting value for given key to null.
5

use a generic List<String>

bool msettingvalue=false;
string[] settings;
var s = new List<String>();
s.AddRange({"setting1","1","setting2","apple"});
if(msettingvalue)
    s.AddRange({"msetting","true"});
settings = s.ToArray();

But... from the look of your array you'd be better off using a different structure to store these things. It's an associative array you want. You could use a Tuple or a Dictionary to model the settings in a way that is easier to handle, and that more accurately reflects the semantics.

bool msettingvalue=false;
var settings = new Dictionary<String,String>();
settings.Add("setting1","1");
settings.Add("setting2","value2");
if(msettingvalue)
    settings.Add({"msetting","true");

...the last two lines could even be.

settings.Add({"msetting",msettingvalue.ToString());

2 Comments

I didn't see that mentioned in the question. Still don't.
I don't think this answer respects the word "inline" used in the title of the question...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.