1

I have the following while statement:

while ((leftSide.Count-rightSide.Count!=-1)&&(leftSide.Count-rightSide.Count!= 0))
{
     // Do stuff here
}

I would love to write this something like this:

while (leftSide.Count - rightSide.Count ! in [-1, 0])
{
     // Do stuff here
}

but that is illegal syntax. I am wondering, is there some way to that? Some syntax I don't know?

I want to see if the difference of the counts in a set of numbers without having to re-include the whole left side of the statement again?

I suppose I could do this:

int x = leftSide.Count-rightSide.Count;
while ((x != -1) && (x != 0))
{
     // Do stuff here
     x = leftSide.Count-rightSide.Count;
}

but I would rather not.

If there is no way to do a "set" comparison does anyone know why? C# is such a full featured language that it seems odd to have something like this missing.

Vaccano
  • 78,325
  • 149
  • 468
  • 850

8 Answers8

8

Using extension methods, you could easily create an In operator:

public static class Extensions
{
    public static Boolean In<T>(this T obj, params T[] items)
    {
        return items.Contains(obj);
    }
}

Usage:

Int32 i = 10;
i.In(10, 20, 30); // True
decyclone
  • 30,394
  • 6
  • 63
  • 80
7

It doesn't need to be in the language, because it can easily be in a library:

private static readonly int[] ValidValues = { -1, 0 };

...

if (!ValidValues.Contains(leftSide.Count - rightSide.Count))
{
    ...
}

I've used an array here because it's so small... but you would want to consider using a HashSet<int> for a large collection.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Once again, you are the king of the quick-draw. Well played Mr. Skeet, well played. – Justin Niessner Feb 25 '11 at 16:36
  • OK, I can use that. I come from a Delphi background where this was part of the language. I guess I just assumed it should be a language level feature in C# too. – Vaccano Feb 25 '11 at 16:43
1
new[]{0,1}.Contains(leftSide.Count - rightSide.Count)
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • This is probably the best C# way to do it. I went with the extension method because it reads a bit better to me. (With my delphi background) – Vaccano Feb 25 '11 at 17:42
0
.Contains

http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx

Kris Ivanov
  • 10,476
  • 1
  • 24
  • 35
0

You can use HashSet (or anything implementing ISet) to do set comparisons. For your purposes, this is overkill.

I suspect that using x>0 would work for your example though.

Jamie Treworgy
  • 23,934
  • 8
  • 76
  • 119
0

For larger amounts, you can declare a List<T> and use the Contains method:

List<int> values = new List<int> {-1, 0};

if(!values.Contains(x))
  //do stuff
Femaref
  • 60,705
  • 7
  • 138
  • 176
0

Use extension methods:

public static class DoubleInExtension
{
    public static bool IsIn(this double x, double a, double b)
    {
        return x >= a && x <= b;
    }
}
Aliostad
  • 80,612
  • 21
  • 160
  • 208
0
int[] set = {-1, 0};
while (!set.Contains(leftSide.Count - rightSide.Count))
{
//Do stuff here
}
Peter Olson
  • 139,199
  • 49
  • 202
  • 242