Question

Does exist to make short code which i can replace if() argument for example:

int x = 1; // x can be 1,2,3 etc.
if(x==1 || x==3 || x==12)
{
     //do something..
}

I don't want to repeat x==1, x==3, etc. just compare numbers to x.

Était-ce utile?

La solution

You can have the possible numbers in an array and then compare it like:

int x = 1;
int[] compareArray = new[] { 1, 3, 12, 33 };
if (compareArray.Contains(x))
{
    //condition met
}

Or you can use Enumerable.Any like:

if (compareArray.Any(r=> r == x))

Autres conseils

You can do it fairly succinctly with a switch statement:

int x= 1;
switch (x)
{
    case 1:  
    case 2:
    case 3:
    case 4:
        Console.WriteLine("x is either 1, 2, 3, or 4");
        break
    default:
        Console.WriteLine("x is not 1, 2, 3, or 4");
        break;
}

Late to the party, and I would definitely, definitely suggest that one use a simple solution such as Habib's answer (and even simplifying it with extension methods). That said, I was curious if it were possible to achieve a minimal syntax along the lines of what Tagon may have been looking for. That is, would it be possible to have something like:

int x = 1;
if(x == 1 || 3 || 12 || 33)
{

}

I suspect there is unneeded verbosity with some of the operators I used here (and certainly some best-practices violations), but something like this is possible with operator overloads. The resulting syntax is:

IntComparer x = 1; //notice the usage of IntComparer instead of int
if(x == 1 || 3 || 12 || 33)
{

}

First I have a kind of "entry point" into the comparison:

public class IntComparer
{
    public int Value { get; private set; }

    public IntComparer(int value)
    {
        this.Value = value;
    }

    public static implicit operator IntComparer(int value)
    {
        return new IntComparer(value);
    }

    public static BoolComparer operator ==(IntComparer comparer, int value)
    {
        return new BoolComparer(comparer.Value, comparer.Value == value);
    }

    public static BoolComparer operator !=(IntComparer comparer, int value)
    {
        return new BoolComparer(comparer.Value, comparer.Value != value);
    }
}

This satisfies the initial x == 1 check. From here on, it switches to a new type BoolComparer:

public class BoolComparer
{
    public int Value { get; private set; }
    public bool IsSatisfied { get; private set; }

    public BoolComparer(int value, bool isSatisfied)
    {
        this.Value = value;
        this.IsSatisfied = isSatisfied;
    }

    public static bool operator true(BoolComparer comparer)
    {
        return comparer.IsSatisfied;
    }

    public static bool operator false(BoolComparer comparer)
    {
        return !comparer.IsSatisfied;
    }

    public static implicit operator bool(BoolComparer comparer)
    {
        return comparer.IsSatisfied;
    }

    public static BoolComparer operator |(BoolComparer comparer, BoolComparer value)
    {
        return new BoolComparer(comparer.Value, comparer.Value == value.Value);
    }

    public static implicit operator BoolComparer(int value)
    {
        return new BoolComparer(value, false);
    }
}

This satisfies the subsequent || 3 || 12 || 33 checks and the final true/false evaluation for the if statement.

These two classes working in conjunction makes the syntax tomfoolery work. It's terrible. Do not use it. Also, it doesn't work for for the negative: if (x != 1 || 3 || 12 || 33). That might be a simple fix, but I don't want to dive into that at the moment)

Some working checks:

IntComparer x = 1;
bool check1 = x == 1 || 3 || 12 || 33; //true
bool check2 = x == 2 || 3 || 12 || 33; //false
bool check3 = x == 5 || 1 || Int32.MaxValue; //true
bool check4 = x == -1; //false
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top