Is there a C# IN operator?
If you wanted to write .In then you could create an extension that allows you to do that.
static class Extensions
{
public static bool In<T>(this T item, params T[] items)
{
if (items == null)
throw new ArgumentNullException("items");
return items.Contains(item);
}
}
class Program
{
static void Main()
{
int myValue = 1;
if (myValue.In(1, 2, 3))
// Do Somthing...
string ds = "Bob";
if (ds.In("andy", "joel", "matt"))
// Do Someting...
}
}
List.Contains()
is I think what you're looking for. C# has in
keyword
and not an operator
which serves completely different purpose then what you're referring in SQL.
There are two ways you can use in
keyword in C#. Assume you have a string[] or List in C#.
string[] names; //assume there are some names;
//find all names that start with "a"
var results = from str in names
where str.StartsWith("a")
select str;
//iterate through all names in results and print
foreach (string name in results)
{
Console.WriteLine(name);
}
Referring your edit, I'd put your code this way to do what you need.
int myValue = 1;
List<int> checkValues = new List<int> { 1, 2, 3 };
if (checkValues.Contains(myValue))
// Do something