简体   繁体   中英

How to declare the values of arrays in a switch statement in C#?

I'm new to C# and I'm trying to figure out how I declare the values of arrays in switch statements. I'm used to using PHP and Javascript often and I can't figure out how I'd have to do this in C#. This is what I have right now:

string winkel = winkelDropdown.SelectedValue;
ArrayList products = new ArrayList();
output.Text = "Bij de " + winkel + " moet ik dit meenemen:<br />";
switch (winkel)
{        
    case "Albert Heijn":
        products.Add("Boter");
        products.Add("Kaas");
        products.Add("Eieren");
        break;
    case "Jumbo":
        products.Add("Spek");
        products.Add("Lamsvlees");
        products.Add("Huiswijn");
        break;
    case "Plus":
        products.Add("Spaghetti");
        products.Add("Pastasaus");
        products.Add("Kaasbroodje");
        break;
    case "Emté":
        products.Add("Jupiler Krat");
        products.Add("Barbeque kolen");
        products.Add("Frisdrank");
        break;
}
foreach (string product in products)
{
    output.Text += product + "<br />";
}

Is there a way in C# to make my switch statement shorter? For example something like this:

string[] productArray = string[3];
switch (winkel)
{        
    case "Albert Heijn":
        productArray = ["Boter", "Kaas", "Eieren"];
        break;
    case "Jumbo":
        productArray = ["Spek", "Lamsvlees", "Huiswijn"];
        break;
    case "Plus":
        productArray = ["Spaghetti", "Pastasaus", "Kaasbroodje"];
        break;
    case "Emté":
        productArray = ["Jupiler Krat", "Barbeque kolen", "Frisdrank"];
        break;
}

First off, I would use List<T> instead of ArrayList .

Second, if you want to add a range of items, use AddRange() and pass in a new string array:

string winkel = winkelDropdown.SelectedValue;
List<string> products = new List<string>();
switch (winkel)
{        
    case "Albert Heijn":
        products.AddRange(new [] { "Boter", "Kaas", "Eieren" });
        break;
    case "Jumbo":
        products.AddRange(new [] { "Spek", "Lamsvlees", "Huiswijn" });
        break;
    //etc
}

You can use AddRange for this:

string winkel = winkelDropdown.SelectedValue;
ArrayList products = new ArrayList();
output.Text = "Bij de " + winkel + " moet ik dit meenemen:<br />";
switch (winkel)
{        
    case "Albert Heijn":
        products.AddRange(new string[] {"Boter", "Kaas", "Eieren" });
        break;
        // ...
}

you almost got it :)

        string[] productArray;
        switch (winkel)
        {
            case "Albert Heijn":
                productArray = new[] {"Boter", "Kaas", "Eieren"};
                break;
            case "Jumbo":
                productArray = new[] { "Spek", "Lamsvlees", "Huiswijn"};
                break;
            case "Plus":
                productArray = new[] { "Spaghetti", "Pastasaus", "Kaasbroodje"};
                break;
            case "Emté":
                productArray = new[] {"Jupiler Krat", "Barbeque kolen", "Frisdrank"};
                break;
        }

Per your posted code, you can't bypass the switch statement but as already answered you can use AddRange() method to add list of string at once. Finally, it's awful using ArrayList instead use List<string> like

List<string> products = new List<string>();

Use Dictionary

var winkelToProducts = new Dictionary<string, string[]>();
// init winkel to its' products
var products = winkelToProducts[winkel];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM