0

我将如何检查一个变量是否是几个值之一?

IN甲骨文中它会读起来像

where strVar in ('A','H','X','Z')

不用写

if (strVar == "A" || strVar == "H" || strVar == "X" || strVar =="Z") 

编辑以更改变量名称

4

3 回答 3

7

这对你来说应该足够了:

if(new string[]{"A","B",".."}.Contains(strVar)) {
    .... CONTAINS ...
}

也可以用

if(new string[]{"A","B",".."}.Any(strVar)) {
    .... CONTAINS ...
}

Contains也适用于C# 2.0,而Any不是。

编辑

正如@brendan 正确指出的那样,摆脱var命名变量,即C#关键字,让我们避免造成混淆。

于 2012-08-29T22:02:33.030 回答
2

这可能是你最好的选择:

if ("hxza".Contains(value))

我用一些不同的方法做了一些“测试速度”来做你想做的事情。

您可以在底部看到代码。请注意,X 是我重复测试以求平均值的次数

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

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

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

所以我们可以说测试 2、7 和 10 是制作你想要的东西的好方法:

//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))

见代码:

            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();
于 2012-08-29T23:13:16.013 回答
0

没有任何上下文:

new [] {'A','H','X','Z'}.Contains(var);

于 2012-08-29T22:02:44.623 回答