简体   繁体   English

可空颜色 - 指定默认值

[英]Nullable Color - assign default value

I have created a small interface IXnaDraw for my assets to determine how to draw themselves in XNA. 我为我的资产创建了一个小型界面IXnaDraw ,以确定如何在XNA中绘制自己。 The interface consists of a single method 界面由单个方法组成

interface IXnaDraw
{
    void Draw(SpriteBatch SB);
}

I wanted to pass a Microsoft.Xna.Framework.Color -object along as I have a change of context and this needs a different color-scheme to be applied to the assets. 我想传递一个Microsoft.Xna.Framework.Color -object,因为我更改了上下文,这需要一个不同的颜色方案来应用于资产。

I tried to use a nullable Color-object like in 我尝试使用像中的nullable颜色对象

void Draw(SpriteBatch SB, Color? Col = null);

The default-parameter is not accepted, I get the error that my classes do not implement said interface. 不接受default-parameter,我得到的错误是我的类没有实现所述接口。 I also tried to use things like 我也尝试过像这样的东西

void Draw(SpriteBatch SB, Color Col = Color.White);

this invokes an error stating 这会引发错误说明

The value must be known at compile-time. 必须在编译时知道该值。

How can I fix the implementation and have my default-parameter? 如何修复实现并使用我的default-parameter? Thank you 谢谢

I assume the 4.0 or higher .NET 我假设4.0或更高版本的.NET

void Draw(SpriteBatch SB, Color Col = default(Color));

or 要么

void Draw(SpriteBatch SB, [Optional]Color Col);

If you want to accept a second parameter, you need to create a second overload of the method that takes two parameters. 如果要接受第二个参数,则需要创建带有两个参数的方法的第二个重载。
Interface implementations must match exactly; 接口实现必须完全匹配; the CLR does not recognize optional parameters. CLR无法识别可选参数。

You can make the single-parameter version call the other overload, if you want to. 如果需要,可以使单参数版本调用另一个重载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM