Reversed if check

c#, if-statement

Solution

If you have a huge list you might consider some list structure.

static Dictionary<int, int> exchange = new Dictionary<int, int>();

static Constructor()
{
    AddExchangePair(4, 10);
    AddExchangePair(3, 12);
    ...
}

static void AddExchangePair(int a, int b)
{
    exchange.Add(a,b);
    exchange.Add(b,a);
}

public staic bool Exchange(ref int value)
{
    int newValue = 0;
    bool exchanged = exchange.TryGetValue(value, out newValue);
    if (exchanged) value = newValue;
    return exchanged;
}

This works for huge lists of exchange pairs.

If you call AddExchangePair with a duplicate number e.g. (7,14) and (14, 16) you will get an exception. You might have to consider what to do in that case.

Problem

I have a huge list of checks that checks for example if integer is 4 or 10, if it is 4 it changes this int to 10 and Vice versa so my if check would be something like this: ``` int i = getval(); if (i == 4) { i = 10; } else if (i == 10) { i = 4; } ``` My question is there another way to do this without the need to check for each condition.

Original source