[Solved] C# Multiple String Compare [closed]


If those strings are the same for all the cases – make them global:

string a = "test";
string b = "try";
string c = "compare";
//case 1
for (int i = 1; i <= 2; i++)
{
     if (a == b) { do something };
     if (a == c) { do something};
 }
 //case 2
 for (int i = 3; i <= 4; i++)
{
     if (a == b) { do something };
     if (a == c) { do something};
}

Doing this you will avoid repeating initializing this strings in every case

Edit 1

you can create function to select values:

for (int i = 1; i <= 2; i++)
{
     bw.Write(SelectDataForBinaryWriter("A"));
}
...
private int SelectDataForBinaryWriter(string input)
{
    int output = 0;
    switch (input) // edit your own cases here
    {
        case "A":
            output = 0x123;
            break;
        case "B":
            output = 0x456;
            break;
        case "C":
            output = 0x789;
            break;
        default:
            output = some_default_value_here;
            break;
    }

    return output;
}

As I can see, you are writing hex values, so you can also consider returning already parsed value – C# convert integer to hex and back again

3

solved C# Multiple String Compare [closed]