Sometimes i need to use != two, three or four times is there a better way to do it?
if (result != 1 && result != 12)
{
do something
}
if (result != 1 && != 12)
{
do something
}
second option would be better, nut we all know it doesn't work
Sometimes i need to use != two, three or four times is there a better way to do it?
if (result != 1 && result != 12)
{
do something
}
if (result != 1 && != 12)
{
do something
}
second option would be better, nut we all know it doesn't work
There is not a direct way in the language like the one you suggested:
// Wont compile
if (result != 1 && != 12) { }
If you only have a few values to check, I'd use the explicit comparsions. However you can create a collection of values an check if the result is not in the colleciton:
// using System.Linq;
if (!(new []{ 1, 2 /*, ... */ }).Contains(result)) { }
As suggested in the comments you can also write an extension method. This requires a public static class:
public static class ExtensionMethods
{
public static bool NotIn(this int num, params int[] numbers)
{
return !numbers.Contains(num);
}
}
// usage
result.NotIn(1, 12);
result.NotIn(1, 12, 3, 5, 6);
And if you want to compare not only integers, you can write a generic method:
public static bool NotIn<T>(this T element, params T[] collection)
{
return !collection.Contains(element);
}
// Works with different types
result.NotIn(1, 2, 3, 4);
"a".NotIn("b", "c", "aa");
You can use Linq All()
method probably
if((new int[]{1,12,13}).All(x => x != result))
{
// do something
}