简体   繁体   中英

Nullable Color - assign default value

I have created a small interface IXnaDraw for my assets to determine how to draw themselves in 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.

I tried to use a nullable Color-object like in

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. 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? Thank you

I assume the 4.0 or higher .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.

You can make the single-parameter version call the other overload, if you want to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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