C# 带字符串的Switch语句#

C# 带字符串的Switch语句#,c#,string,switch-statement,C#,String,Switch Statement,我需要写一些东西来获取启动参数,然后为那些启动参数做一些事情,我认为switch会很好,但它只接受int,必须是字符串 这不是实际的代码,但我想知道如何使类似的工作 namespace Simtho { class Program { static void Main(string[] args) { switch (Environment.GetCommandLineArgs()) {

我需要写一些东西来获取启动参数,然后为那些启动参数做一些事情,我认为switch会很好,但它只接受int,必须是字符串

这不是实际的代码,但我想知道如何使类似的工作

namespace Simtho
{
    class Program
    {
        static void Main(string[] args)
        {
            switch (Environment.GetCommandLineArgs())
            {

                case "-i":
                    Console.WriteLine("Command Executed Successfully");
                    Console.Read;
                    break;
            }
        }

    }
}

像这样的怎么样

string[] args = Environment.GetCommandLineArgs();

if (args.Contains("-i"))
{
    // Do something
}

GetCommandLineArgs()返回字符串数组

也许我错了,但在内部打开转换为if-else序列的字符串…

返回一个
字符串[]


不能打开字符串数组。不过,您可能需要测试数组是否包含某些值。

Environment.GetCommandLineArgs()返回字符串数组。无法打开阵列。尝试迭代数组的成员,如下所示:

namespace Simtho
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string arg in Environment.GetCommandLineArgs())
            {
                switch (arg)
                {

                    case "-i":
                        Console.WriteLine("Command Executed Successfully");
                        Console.Read();
                        break;
                }
            }
        }
    }
}

你可以很好地打开字符串。