我想做一个函数,在调用时改变控制台的颜色,以及我选择的那些颜色。我不想每次都写3条指令,所以这就是为什么我希望它在函数中。如何使用变量完成指令?
到目前为止,我已经做了一些与此类似:
public static void WindowColor(string Background, string Foreground)
{
Console.BackgroundColor = ConsoleColor.Background;
Console.ForegroundColor = ConsoleColor.Foreground;
Console.Clear();
}
static void Main(string[] args)
{
WindowColor("DarkCyan","White");
}
其中ConsoleColor.Background
和ConsoleColor.Foreground
我想与ConsoleColor.DarkCyan
和ConsoleColor.White
被替换,因为我在WindowColor("DarkCyan","White");
调用。
但我得到这个错误:
'ConsoleColor' does not contain a definition for 'Background'.
现在我得到的Background
在ConsoleColor.Background
不被视为一个变量,而是作为指令的一部分的事实,但问题是:我怎么能使Background
或Foreground
被视为以变量的形式完成指令?
为什么不使用'WindowColor(ConsoleColor.DarkCyan,ConsoleColor.White)'而不是?或者在C#6和'WindowColor(DarkCyan,White)'中使用'static''? (并将每个参数的类型更改为“ConsoleColor”,理想情况下将其重命名为惯用语言。)您是否希望*删除编译时检查? –
我不知道我能做到这一点,我刚开始使用C#。但它解决了我的问题。谢谢。 – BatchGuy