C# 如何使用变量完成指令?
我想创建一个函数,在调用时使用我选择的颜色更改控制台的颜色。我不想每次都写3条指令,所以我希望它在函数中 到目前为止,我已经做了类似的事情:C# 如何使用变量完成指令?,c#,.net,C#,.net,我想创建一个函数,在调用时使用我选择的颜色更改控制台的颜色。我不想每次都写3条指令,所以我希望它在函数中 到目前为止,我已经做了类似的事情: public static void WindowColor(string Background, string Foreground) { Console.BackgroundColor = ConsoleColor.Background; Console.ForegroundColor = ConsoleColor.Foreground
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”不包含“Background”的定义
现在我得到了一个事实,即控制台颜色中的背景。背景不被视为一个变量,而是作为指令的一部分,但问题是:如何将背景或前景视为以变量形式完成指令?通常,您只需使用正确类型的参数而不是字符串:
public static void WindowColor(ConsoleColor background, ConsoleColor foreground)
{
Console.BackgroundColor = background;
Console.ForegroundColor = foreground;
Console.Clear();
}
static void Main(string[] args)
{
WindowColor(ConsoleColor.DarkCyan, ConsoleColor.White);
}
如果坚持使用字符串作为参数,则必须解析它们:
public static void WindowColor(string Background, string Foreground)
{
Console.BackgroundColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), Background, true);
Console.ForegroundColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), Foreground, true);
Console.Clear();
}
static void Main(string[] args)
{
WindowColor("DarkCyan","White");
}
通常,您只需使用正确类型的参数而不是字符串:
public static void WindowColor(ConsoleColor background, ConsoleColor foreground)
{
Console.BackgroundColor = background;
Console.ForegroundColor = foreground;
Console.Clear();
}
static void Main(string[] args)
{
WindowColor(ConsoleColor.DarkCyan, ConsoleColor.White);
}
如果坚持使用字符串作为参数,则必须解析它们:
public static void WindowColor(string Background, string Foreground)
{
Console.BackgroundColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), Background, true);
Console.ForegroundColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), Foreground, true);
Console.Clear();
}
static void Main(string[] args)
{
WindowColor("DarkCyan","White");
}
Console.ForegroundColor
和Console.BackgroundColor
是类型,而不是字符串。如果希望函数正常工作,则需要将参数类型更改为ConsoleColor:
public static void WindowColor(ConsoleColor Background, ConsoleColor Foreground)
{
Console.BackgroundColor = Background;
Console.ForegroundColor = Foreground;
Console.Clear();
}
或者,您可以将其保留为字符串,然后尝试将该字符串解析为枚举值:
public static void WindowColor(string Background, string Foreground)
{
ConsoleColor b, f;
if ( Enum.TryParse(Background, out b)
&& Enum.TryParse(Foreground, out f))
{
Console.BackgroundColor = b;
Console.ForegroundColor = f;
Console.Clear();
}
}
Console.ForegroundColor
和Console.BackgroundColor
是类型,而不是字符串。如果希望函数正常工作,则需要将参数类型更改为ConsoleColor:
public static void WindowColor(ConsoleColor Background, ConsoleColor Foreground)
{
Console.BackgroundColor = Background;
Console.ForegroundColor = Foreground;
Console.Clear();
}
或者,您可以将其保留为字符串,然后尝试将该字符串解析为枚举值:
public static void WindowColor(string Background, string Foreground)
{
ConsoleColor b, f;
if ( Enum.TryParse(Background, out b)
&& Enum.TryParse(Foreground, out f))
{
Console.BackgroundColor = b;
Console.ForegroundColor = f;
Console.Clear();
}
}
为什么不使用WindowColor(ConsoleColor.DarkCyan,ConsoleColor.White)
?或者在C#6中使用静态的和窗口颜色(黑色,白色)
?(并将每个参数的类型更改为ConsoleColor
,理想的情况是同时将其重命名为惯用形式。)是否要删除编译时检查?我不知道我能做到,我刚开始使用C。但它解决了我的问题。谢谢。为什么不使用WindowColor(ConsoleColor.DarkCyan,ConsoleColor.White)
?或者在C#6中使用静态的和窗口颜色(黑色,白色)
?(并将每个参数的类型更改为ConsoleColor
,理想的情况是同时将其重命名为惯用形式。)是否要删除编译时检查?我不知道我能做到,我刚开始使用C。但它解决了我的问题。谢谢,我刚发现我能做到。谢谢你的回答!仅仅使用Parse
是危险的。如果字符串不能被解析,这将抛出一个错误。我刚刚发现我可以这样做。谢谢你的回答!仅仅使用Parse
是危险的。如果无法解析字符串,则会引发错误。