0

I'm sure the answer is going to be no but I was curious and thought I'd ask you boffins just to be sure.

In SQL you can say:

IF ( a IN ( x, y ) )

Is there something similar in C#? I know you can use a switch but I'm looking for something specifically possible in an if statement due to having multiple expressions like:

IF ( a == b && c IN ( x, y ) )
IF ( a == b && c == ( x || y ) )

Edit: Ok, I see I oversimplified as the array example is rather obvious and I didn't think it through. How about if it's something more complex like:

if ( a.GetType() == typeof( MyClass1 ) || a.GetType() == typeof( MyClass2 ) )

Edit2: Just to clarify, this is not a duplicate of Is there a C# IN operator? as the answers to that question don't address my question as the example I used was maybe a little poor but it was the only relation I could think of at the time. The answer provided by Palani Kumar is the answer I was looking for.

Edit3: Thanks to everyone for weighing in. It's quite clear that there are numerous ways to achieve the same result (I also understand now that they're pretty much the same thing which is just being expressed differently) and I have been able to read further into each one thanks to your input. I also managed to find this "if statements matching multiple values" post which yes, I fully agree that it too is a duplicate of what I asked. Based on the feedback I've not only learnt a lot but managed to also implement an elegant solution being:

public static bool In<T>( this T obj, params T[] list )
{
    return list.Contains( obj );
}
Community
  • 1
  • 1
Storm
  • 1,848
  • 4
  • 20
  • 39
  • 1
    What is the type of `c` for example? – Soner Gönül Jul 28 '15 at 11:34
  • To be fair, I think @PalaniKumar answer is *effectively* the same as the answer in the marked duplicate. Creating a collection and using `.Any` is essentially the same as creating an array and using `.Contains`. – Matt Burland Jul 28 '15 at 17:58
  • @MattBurland Thanks Matt, I also reflected on it last night and it did start to make sense. I did find some related material AFTER I asked my question but before, when I did a bunch of searching, I wasn't able to find anything which is why I thought the answer would be no and hence I asked – Storm Jul 29 '15 at 06:08
  • @MattBurland and Storm .Contains will not work for all the cases but .Any will work. For example, In my answer the "Sample2" will not work with .Contains. Also cannot ignore case sensitive with .Contains but can in .Any. So it is not same as the answer in the marked duplicate :) – Palanikumar Aug 11 '15 at 11:29
  • @PalaniKumar wow, that's some interesting stuff, definitely going to read up more on that. Thanks! – Storm Aug 12 '15 at 11:48

2 Answers2

6

Contains should fit your needs

List<char> validChars = new List<char>() { 'x', 'y' };
char a = 'y';

if (validChars.Contains(a))
{
    //do sth.
}

Update to the updated question: handling Type instead of char

List<Type> validTypes = new List<Type>(){ typeof(MyClass1)  ,typeof( MyClass2 )};   
MyClass1 a = new MyClass1();  

if (validTypes.Contains(a.GetType()))
{
    //do sth.
}
fubo
  • 44,811
  • 17
  • 103
  • 137
  • Or if it is an array, use `Array.IndexOf`. – Patrick Hofman Jul 28 '15 at 11:37
  • 3
    See also http://stackoverflow.com/a/833477/1336590 – Corak Jul 28 '15 at 11:37
  • Thanks @Fubo, what about when it becomes more complex? I've edited my question to try display more or less what prompted the original question. – Storm Jul 28 '15 at 11:48
  • 1
    @Storm: Your edited example is no more complex than this answer. You just have a collection of `Type` instead of a collection of `char`. You *could* use exactly the same approach. – Matt Burland Jul 28 '15 at 18:01
1

Sample1:

if (new[] { "String1", "String2" }.Any(x => x == "MyValue"))
{
}

Sample2:

if (new[] { new { Prop1 = "Value1" }, new { Prop1 = "Value2" } }.Any(x => x.Prop1 == "MyValue"))
{
}

Sample3:

if (new[] { typeof(MyClass1), typeof(MyClass2) }.Any(x => x == a.GetType()))
{
}
Palanikumar
  • 6,940
  • 4
  • 40
  • 51