1

In C#, I have a string array of values. However, based on a parameter, the array should not include two of the values, but include all the same other values. What's the proper way to approach this? (I could use a list...but I'm hoping there's a better way)

Example code:

bool msettingvalue=false;
string[] settings;
if(msettingvalue)
    settings = new string[]{
        "setting1","1",
        "setting2","apple",
        "msetting","true"};
else
    settings = new string[]{
        "setting1","1",
        "setting2","apple"};
//code that goes through settings

What's the proper way to assign the array to settings. All the values will be same, it's that if the msettingvalue is false, it should include certain settings. (You may change the title if you think of a better one).

Edit1 Ideally, I'd like to stay with a string[] array vs a dictionary as its faster...and I'm iterating through the array in order so I don't need a key index.

Edit2 I've asked a followup question in hopes of there being a way to use an inline conditional for this.

1
  • Look at my answer, I have just edited it, maybe it will be ok for you Commented Oct 17, 2011 at 1:02

4 Answers 4

10

I have a strong opinion that you should be using Dictionary<string, string> instead.

Then you could have:

bool msettingValue = false;
Dictionary<string, string> settings = new Dictionary<string, string> {
    {"setting1", "1"},
    {"setting2", "apple"},
}
if(msettingValue) {
    settings.Add("msetting", "true");
}
Sign up to request clarification or add additional context in comments.

3 Comments

I know I can use a dictionary/list...but I'd like to see if there's anything for a string[] array...it's faster.
@user389823: You say it's faster, but all the mechanisms you'll end up adding to mimic a dictionary (cause that's exactly what it is) is going to be as CPU Time expensive.
@user389823, that is called "premature optimization" (, a bad habit that is hard to get rid of). What is cheaper: more CPU; or more developer time?
1

If you really don't want to use dictionary, you can do take this function:

    private static string[] AddSettings(ref string[] settings, params string[] SettingsToAdd)
    {                   
        int length = SettingsToAdd.Length;

        if (length%2 !=0)
        {
            throw new ArgumentException("Invalid number of elements");
        }

        int OldLength = settings.Length;
        int NewLength = settings.Length + length;
        Array.Resize(ref settings, NewLength);
        for (int i = OldLength; i < NewLength; i++)
        {
            settings[i] = SettingsToAdd[i - OldLength];
        }
        return settings;
    }

EDIT: Or this version, based on an answer of your other post:

    private static string[] AddSettings(ref string[] settings, params string[] SettingsToAdd)
    {                   
        int length = SettingsToAdd.Length;

        if (length%2 !=0)
        {
            throw new ArgumentException("Invalid number of elements");
        }
        else
            return settings.Concat(SettingsToAdd).ToArray();
    }

and its use:

        bool msettingvalue = true;
        string[] settings;
        settings = new string[]{
            "setting1","1",
            "setting2","apple"};
        if (msettingvalue)
        {
            settings= AddSettings(ref settings,"setting1", "value1", "setting2", "value2", "setting3", "value3");
        }

You can then add as many values as you wish, and the count must be a multiple of 2, if not, it throws an exception

1 Comment

Array.Resize copies the entire array to a new array with that size (and hence is slow)...it'd be faster to use a dictionary.
1

You could go with something like this :

Dictionary<string,string> settings = new Dictionary<string,string>();
if (msettingValue) {
   settings["msetting"] = "true";
}
settings["setting1"] = "1";
settings["setting2"] = "apple";

Comments

0

Why not build the array with the common elements, then add the extra stuff if the bool settings variable is true?

Comments

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.