c# - 带字符串的 Switch 语句 C#

标签 c# string switch-statement

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

这不是实际的代码,但我想知道如何实现这样的功能

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

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

    }
}

最佳答案

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;
                }
            }
        }
    }
}

关于c# - 带字符串的 Switch 语句 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1735439/

相关文章:

c# - 我需要重复的测试方法吗? 1 个用于单元测试,1 个用于集成测试?

c# - 何时分配类型?

c# - 我的代码是否正确清理了它的 List<MemoryStream>?

c++ - 为什么不能在 switch 语句中声明变量?

C++ 开关语句

swift - 旋转设备时切换到另一个 View Controller

c# - 加密 Web.Config (Web.Release.config) 使用 aspnet_regiis 转换文件

arrays - 如何将 int 数组转换为字符串数组

c# - IRC聊天命令解析

r - 如何从字母向量枚举字符串