我可以指定在C#4.0中的默认颜色参数?

编程入门 行业动态 更新时间:2024-10-27 09:41:29
本文介绍了我可以指定在C#4.0中的默认颜色参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面是一个例子功能:

public void DrawSquare(int x, int y, Color boxColor = Color.Black) { //Code to draw the square goes here }

编译器不断给我的错误:则是'boxColor'must是一个编译时间常数

The compiler keeps giving me the error: Default parameter value for 'boxColor'must be a compile-time constant

我曾尝试

Color.Black, Color.FromKnownColor(KnownColor.Black), and Color.FromArgb(0, 0, 0)

我如何Color.Black是默认颜色?另外,我不想使用字符串黑来指定它(我知道会的工作)。我想在 Color.Black 值。

How do I make Color.Black be the default color? Also, I do not want to use a string Black to specify it (which I know would work). I want the Color.Black value.

推荐答案

Color.Black 是一个静态的,不是一个恒定的,所以,不,你不能做到这一点。

Color.Black is a static, not a constant, so no, you cannot do this.

要使用默认值,可以使参数为空的(颜色?),如果为空,然后将其设置为黑色。

To use a default, you can make the parameter nullable (Color?), and if it is null, then set it to Black.

更多推荐

我可以指定在C#4.0中的默认颜色参数?

本文发布于:2023-11-12 00:49:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580034.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:颜色   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!