I’m trying to make a method with optional parameter SpriteFont = myFont work. Because spritefont is not a compile time constant, I can’t use it, so I decided to try SpriteFont? = null:
void MyMethod(int x, SpriteFont? font = null)
{
SpriteFont f;
if (font == null) f = defaultFont; else f = font; // Cannot convert type 'Microsoft.Xna.Framework.Graphics.SpriteFont?' to 'Microsoft.Xna.Framework.Graphics.SpriteFont'
}
How do I take out my SpriteFont from SpriteFont??
UPD: this may be a problem with types that are not convertible from nullable to base type by default. (SpriteFont)font still causes “cannot convert”.
UPD2: turns out SpriteFont is nullable by default. Still, I now know how to work with nonNullable?.
Nullable can be converted to the non nullable type by these ways:
(Type)variable;variable.Value