Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# equivalent for Delphi's in

Tags:

c#

delphi

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


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

Thanks

like image 203
Pascal Avatar asked Mar 01 '10 16:03

Pascal


2 Answers

I prefer a method like defined here: Comparing a variable to multiple values

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
like image 176
Gabe Avatar answered Oct 14 '22 23:10

Gabe


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
}
like image 37
Randolpho Avatar answered Oct 14 '22 23:10

Randolpho