Reputation: 3688
just to throw some code out there
string answer = "hotel"
if (answer == "house"|| answer == "hotel" || answer =="appartment")
{
DoSomething()
}
I was wondering if there was some way to shorten it to
string answer = "hotel"
if (answer == "house"|| "hotel" || "appartment")
{
DoSomething()
}
I am aware of the switch statement
switch (answer)
{
case "house":
case "hotel":
case "appartment": DoSomething();
break;
default :DoNothing();
}
I am just wondering if there is some syntax sugar like what I described above.
Upvotes: 3
Views: 322
Reputation: 4492
I would use an extension method like this:
public static bool In<T>(this T item, params T[] items)
{
return items.Contains(item);
}
Use it like this:
if ("hotel".In("house", "hotel", "apartment"))
or
if (1.In(1,2))
Upvotes: 0
Reputation: 14522
public static bool Any<T>(T item, params T[] items)
{
return items.Contains(item);
}
Usage:
if (Any(6, 1, 2, 3, 4, 5, 6, 7))
{
// 6 == 6
}
if (Any("Hi", "a", "cad", "asdf", "hi"))
{
}
else
{
// "Hi" != "hi" and from anything else.
}
Or:
public string[] items = new string[] {"a", "cad", "asdf", "hi"};
...
if (Any("Hi", items))
{
Works just as well.
}
You can also have more advanced comparison. For example, if you wanted:
if (person.Name == p1.Name ||
person.Name == p2.Name ||
person.Name == p3.Name ||
person.Name == p4.Name ||
person.Name == p5.Name || ...)
{
}
You can have:
public static bool Any<T>(T item, Func<T, T, bool> equalityChecker, params T[] items)
{
return items.Any(x => equalityChecker(item, x));
}
And do:
if (Any(person, (per1, per2) => p1.Name == p2.Name, p1, p2, p3, p4, p5, ...)
{
}
EDIT
If you insist, you can make it, of course, an extension method:
public static bool Any<T>(this T item, params T[] items)
{
return items.Contains(item);
}
Usage:
var b = 6.Any(4, 5, 6, 7); // true
And the same logic of adding the keyword "item" in the signature goes for the overload with the equalityChecker.
Upvotes: 4
Reputation: 802
I normally use a switch statement in this case, but as already said in a few answers above you can put the results in an list or array and check it that way.
Upvotes: 0
Reputation: 616
Consider adding an extension method that will accept any strings...
string answer = "hotel"
if (answer.EqualsAny("house", "hotel", "appartment"))
{
DoSomething()
}
// Extending the thought to another version
if (answer.EqualsAll("house", "hotel", "appartment"))
{
DoSomething()
}
public static class Extensions
{
public static bool EqualsAny(this string value, params string[] compareAgainst)
{
foreach (var element in compareAgainst)
{
if(value == element)
return true;
}
return false;
}
public static bool EqualsAll(this string value, params string[] compareAgainst)
{
foreach (var element in compareAgainst)
{
if(value != element)
return false;
}
return true;
}
}
Upvotes: 1
Reputation: 460
If your comparison parameters are fixed, you can opt for ENUM with Flags. Ref MSDN. Then you can have the desired behavior. You can also refer to this post
Upvotes: 0
Reputation: 688
You could use an array and use Contains.
So in your example:
string answer = "hotel";
string[] acceptable = new string[]{"house", "hotel", "appartment"};
if (acceptable.Contains(answer)){
DoSomething();
}
Upvotes: 7
Reputation: 62256
You can do like
if(new string[]{"house","hotel","appartment"}.Contains(asnwer))
{
...
}
or
if(new List<string>(){"house","hotel","appartment"}.Any(x=>x == answer)
{
}
Can add this like an extension method too, and use...
Upvotes: 1
Reputation: 850
Only thing I can think of is to work with a list or array:
List<String> words = new List<String> { "house", "hotel", "appartment" };
String answer = "house";
if (words.Contains(answer))
{
DoSomething();
}
Upvotes: 2
Reputation: 48568
Create a Global List and just check it in any method.
List<string> validAnswers = new List<string> {"house", "house1", "apartment"};
if (validAnswers.Contains(answer))
DoSomething();
In this case your List
will not be generated every time answer
is checked
Upvotes: 3
Reputation: 11885
It is possible to use some syntactic sugar for this:
if((new[]{"house", "hotel", "apartment"}).Contains(answer))
{
}
Note that this will create a new array on the fly, so will potentially be more expensive than just the Boolean
checks.
Upvotes: 8