C# 4.0:我可以使用 Color 作为具有默认值的可选参数吗?

发布于 2024-08-31 19:47:07 字数 566 浏览 3 评论 0原文

    public void log(String msg, Color c = Color.black)
    {
        loggerText.ForeColor = c;
        loggerText.AppendText("\n" + msg);

    }

这会导致错误:c 必须是编译时常量。我已经阅读了一些这方面的内容,大多数例子都是处理字符串和整数。我发现我可以使用 colorconverter 类,但我不确定它是否会非常有效。有没有办法只传递基本颜色作为可选参数?

    public void log(String msg, String c = "Black")
    {
        ColorConverter conv = new ColorConverter();
        Color color = (Color)conv.ConvertFromString(c);

        loggerText.ForeColor = color;
        loggerText.AppendText("\n" + msg);
    }
    public void log(String msg, Color c = Color.black)
    {
        loggerText.ForeColor = c;
        loggerText.AppendText("\n" + msg);

    }

This results in an error that c must be a compile-time constant. I've read up on this a little and most examples are dealing with strings and ints. I've figured out I can use the colorconverter class but I'm not sure it will be very efficient. Is there a way to just pass a basic color as an optional parameter?

    public void log(String msg, String c = "Black")
    {
        ColorConverter conv = new ColorConverter();
        Color color = (Color)conv.ConvertFromString(c);

        loggerText.ForeColor = color;
        loggerText.AppendText("\n" + msg);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

童话里做英雄 2024-09-07 19:47:07

我也遇到过这个问题,我发现的唯一解决方法是使用可空值。

public void log(String msg, Color? c = null)
{
    loggerText.ForeColor = c ?? Color.Black;
    loggerText.AppendText("\n" + msg);
}

其他可能的语法是:

loggerText.ForeColor = c.GetValueOrDefault(Color.Black);

I've run into this as well and the only workaround I've found is to use nullables.

public void log(String msg, Color? c = null)
{
    loggerText.ForeColor = c ?? Color.Black;
    loggerText.AppendText("\n" + msg);
}

Other possible syntax is:

loggerText.ForeColor = c.GetValueOrDefault(Color.Black);
-黛色若梦 2024-09-07 19:47:07

您可以检查 Color 是否为 Color.Empty(这是默认值:default(Color))或使用可为 null 的值并检查是否为 null。

public void log(String msg, Color? c = null) { ... }

You could check if Color is Color.Empty (which is the default value: default(Color)) or use a nullable value and check for null.

public void log(String msg, Color? c = null) { ... }
听不够的曲调 2024-09-07 19:47:07

不要指定颜色。相反,提供“错误级别”,并在每个错误级别和颜色值之间建立映射。这样,0 及以下可能是黑色,然后 1 = 琥珀色,>2 = 红色。无需担心默认值和/或未指定值。

Don't specify the colour. Supply an "error level" instead, and have a mapping between each error level and a colour value. That way 0 and below could be black, then 1 = amber, >2 = red. No need to worry about default values and/or not specifying a value.

何时共饮酒 2024-09-07 19:47:07

使用建议:

public GraphicsLine(Point startPoint, Point endPoint, Color? color = null, double width = 1.0)
{
    StartPoint = startPoint;
    EndPoint = endPoint;
    Color = color ?? Colors.Black;
    Width = width;
}

Usage suggestion:

public GraphicsLine(Point startPoint, Point endPoint, Color? color = null, double width = 1.0)
{
    StartPoint = startPoint;
    EndPoint = endPoint;
    Color = color ?? Colors.Black;
    Width = width;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文