tags:

views:

410

answers:

6

Hi,
What is the equivalent in C# for Delphi's in syntax, like:


  if (iIntVar in [2,96]) then 
  begin
    //some code
  end;

Thanks

+4  A: 

There is no such equivalent. The closest is the Contains() extension method of a collection.

Example:

var vals = new int[] {2, 96};
if(vals.Contains(iIntVar))
{
  // some code
}
Randolpho
`There is no such equivalent`. Isn't `Contains` the same as `in` as they are both simply checking if the value is in the array?
James
Contains only checks for those values not between them see http://www.delphibasics.co.uk/RTL.asp?Name=In
John Nolan
Thanks for your time and answer!
Pascal
@James: `Contains` works the same as `in`, but is not a syntactic idiom controlled by the compiler, it's a library call. There is no such *language* equivalent, only library support.
Randolpho
@John Nolan: @Pascal did not use `[2..96]`, he used `[2,96]`. `[2,96]` creates a list containing the values 2 and 96. Had he used `[2..96]`, it would have created an array containing the values 2, 3, 4, 5, 6, 7, ... , 95, 96. At which point he would have been checking for a value between two numbers, and would have been better off using lower and upper bounds checking rather than array containment, which is what the `in` operator in Delphi does.
Randolpho
Actually [2..96] would have created a *set* containing the values 2..96, which uses a lot fewer bytes to represent, and can be tested in O(1) time, instead of the linear search that an array scan requires.
Mason Wheeler
@Mason Wheeler: Excellent point. In truth, they'd *all* be sets, even `[2,96]`, and good point about constant time for checking a set in Delphi; I'd forgotten all about it. Stupid me spending too much time in C#.
Randolpho
+4  A: 

In .Net, .Contains is the closest, but the syntax is the opposite of what you wrote.

You could write an extension method to be able to create a .In method

public static bool In<T>(this T obj, IEnumerable<T> arr)
 {
  return arr.Contains(obj);
 }

And usage would be

if (42.In(new[] { 12, 42, 46, 74 }) )
{
    //TODO: Something
}
Chad
using a params array makes the syntax a little nicer. 42.In(12,42,46,74);
Mark
Thanks for your time and answer!
Pascal
@Mark, +1, indeed it would, though then again, it depends if your array is already in a variable or not already. IMHO, it should be, as a list of magic numbers is a code smell. In which case, IEnumerable allows you to pass in an array, or List, etc
Chad
@Chad You can still pass in an array, but you would be limited to an array. I agree about the magic numbers. I've used this when checking against an enum (e.g. state.In(Closed,Closing,Unavailable) ...etc)
Mark
+1  A: 

You can create this extension method:

public static class ExtensionMethods
{
    public static bool InRange(this int val, int lower, int upper)
    {
        return val >= lower && val <= upper;
    }
}

then you can do this:

int i = 56;
if (i.InRange(2, 96)) { /* ... */ }
plinth
+1  A: 

You could write an extension method

 public static bool In(this int value, int[] range)
    {
        return (value >= range[0] && value <= range[1]);
    }
John Nolan
+5  A: 

I prefer a method like defined here: http://stackoverflow.com/questions/2356949/comparing-a-variable-to-multiple-values/2357002#2357002

Here's the conversion of Chad's post:

public static bool In(this T obj, params T[] arr)
{
    return arr.Contains(obj);
}

And usage would be

if (intVar.In(12, 42, 46, 74) ) 
{ 
    //TODO: Something 
} 

or

if (42.In(x, y, z))
    // do something
Gabe
Very interesting option. +1.
Randolpho
+1  A: 

To expand upon what Mason Wheeler wrote in a comment, this would be HashSet<T>.Contains (under .NET 3.5).

int i = 96;
var set = new HashSet<int> { 2, 96 };

if (set.Contains(i))
{
   Console.WriteLine("Found!");
}
TrueWill