Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用变量完成指令?_C#_.net - Fatal编程技术网

C# 如何使用变量完成指令?

C# 如何使用变量完成指令?,c#,.net,C#,.net,我想创建一个函数,在调用时使用我选择的颜色更改控制台的颜色。我不想每次都写3条指令,所以我希望它在函数中 到目前为止,我已经做了类似的事情: public static void WindowColor(string Background, string Foreground) { Console.BackgroundColor = ConsoleColor.Background; Console.ForegroundColor = ConsoleColor.Foreground

我想创建一个函数,在调用时使用我选择的颜色更改控制台的颜色。我不想每次都写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”不包含“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
是危险的。如果无法解析字符串,则会引发错误。