我想做一個函數,在調用時改變控制檯的顏色,以及我選擇的那些顏色。我不想每次都寫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