Reputation: 1403
How would I check if a variable is one of a few values?
Where IN
in Oracle it would read like
where strVar in ('A','H','X','Z')
without having to write
if (strVar == "A" || strVar == "H" || strVar == "X" || strVar =="Z")
editted to change variable name
Upvotes: 0
Views: 247
Reputation: 62266
This should be enough for you:
if(new string[]{"A","B",".."}.Contains(strVar)) {
.... CONTAINS ...
}
can use also
if(new string[]{"A","B",".."}.Any(strVar)) {
.... CONTAINS ...
}
but Contains
works also on C# 2.0
, instead Any
no.
EDIT
As correctly pointed out @brendan, get rid of var
named variable, that is C#
keyword, let's avoid creating confusion.
Upvotes: 7
Reputation: 144
This should probably be you best bet:
if ("hxza".Contains(value))
I made somes "test speed" with some diferents ways to do what you whant to do.
You can see the code at bottom. Note that X is the number of time i repeat the test to make an average
Result for X = 1:
Average of ticks for test7 : 4
Average of ticks for test2 : 5
Average of ticks for test10 : 6
Average of ticks for test9 : 7
Average of ticks for test4 : 19
Average of ticks for test8 : 35
Average of ticks for test6 : 451
Average of ticks for test5 : 2711
Average of ticks for test1 : 3146
Average of ticks for test3 : 8569
Result for X = 100:
Average of ticks for test2 : 3
Average of ticks for test7 : 3
Average of ticks for test10 : 3
Average of ticks for test9 : 5
Average of ticks for test4 : 5
Average of ticks for test8 : 5
Average of ticks for test6 : 11
Average of ticks for test3 : 20
Average of ticks for test5 : 23
Average of ticks for test1 : 35
Result for X = 100 000:
Average of ticks for test2 : 3
Average of ticks for test7 : 3
Average of ticks for test5 : 3
Average of ticks for test1 : 3
Average of ticks for test6 : 3
Average of ticks for test10 : 3
Average of ticks for test9 : 5
Average of ticks for test4 : 5
Average of ticks for test8 : 5
Average of ticks for test3 : 5
So we could tell that test 2, 7 and 10 are good way to make what you want:
//test2
if (value == 'a' || value == 'h' || value == 'x' || value == 'z')
//test7
if (value == 'h' || value == 'x' || value == 'z' || value == 'a')
//test10
if ("hxza".Contains(value))
See the code:
char value = 'a';
Dictionary<string, long> tests = new Dictionary<string, long>()
{
{"test1", 0},
{"test2", 0},
{"test3", 0},
{"test4", 0},
{"test5", 0},
{"test6", 0},
{"test7", 0},
{"test8", 0},
{"test9", 0},
{"test10", 0}
};
Stopwatch watch = new Stopwatch();
// tested with 1, 100 and 100 000
long X = 1;
for (int i = 0; i < X; i++)
{
watch.Start();
if ("ahxz".Any(c => c == value))
{
}
watch.Stop();
tests["test1"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if (value == 'a' || value == 'h' || value == 'x' || value == 'z')
{
}
watch.Stop();
tests["test2"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if (new[] { 'a', 'h', 'x', 'z' }.Contains(value))
{
}
watch.Stop();
tests["test3"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if (new[] { 'a', 'h', 'x', 'z' }.Contains(value))
{
}
watch.Stop();
tests["test4"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if ("ahxz".Contains(value))
{
}
watch.Stop();
tests["test5"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if ("hxza".Any(c => c == value))
{
}
watch.Stop();
tests["test6"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if (value == 'h' || value == 'x' || value == 'z' || value == 'a')
{
}
watch.Stop();
tests["test7"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if (new[] { 'h', 'x', 'z', 'a' }.Contains(value))
{
}
watch.Stop();
tests["test8"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if (new[] { 'h', 'x', 'z', 'a' }.Contains(value))
{
}
watch.Stop();
tests["test9"] += watch.ElapsedTicks;
watch.Reset();
watch.Start();
if ("hxza".Contains(value))
{
}
watch.Stop();
tests["test10"] += watch.ElapsedTicks;
watch.Reset();
}
foreach (var test in tests.OrderBy(p => p.Value))
{
Console.WriteLine("Average of ticks for {0} : {1}\n", test.Key, test.Value / nbrOfTimes);
}
Console.ReadLine();
Upvotes: 2
Reputation: 17388
Without any context:
new [] {'A','H','X','Z'}.Contains(var);
Upvotes: 0