简体   繁体   English

控制台命令C#中的var

[英]var in console command C#

How can I insert a var in console command? 如何在控制台命令中插入var? i mean somthing like that: 我的意思是这样的:

string color = Console.readline();
Console.ForegroundColor = ConsoleColor.color;

You are going to have to convert the string to a ConsoleColor. 您将不得不将字符串转换为ConsoleColor。 Like this: 像这样:

using System;

class Program {
    static void Main(string[] args) {
        var colorName = Console.ReadLine();
        try {
            ConsoleColor color = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorName, true);
            if (color == Console.BackgroundColor) throw new ArgumentException("That would make invisible output");
            Console.ForegroundColor = color;
            Console.WriteLine("Okay");
        }
        catch (ArgumentException ex) {
            Console.WriteLine(ex.Message);
        }
        Console.ReadLine();
    }
}

Not the greatest solution, the French won't understand why "Blanc" doesn't work. 这不是最大的解决方案,法国人不明白“ Blanc”为什么不起作用。

Do you mean this: 您的意思是:

string color = Console.ReadLine();
Console.ForegroundColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), color);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM