See Can I specify a default Color parameter in C# 4.0?
Also: Default arguments for structures
As described in Section 7.15, a constant-expression is an expression that can be fully evaluated at compile-time. Since the only way to create a non-null value of a reference-type other than string is to apply the new operator, and since the new operator is not permitted in a constant-expression, the only possible value for constants of reference-types other than string is null.
In other words, at compile time, you can only default to:
- null
- a literal string
- new T() // no arguments, or equivalently default(T)
Since you can only use new if you are using default arguments, and you can't do that with a reference type (struct) and because you need to specify some specific arguments anyway, you're only option is to pass null.
const Vector4 colorBlack = new Vector4(0,0,0,1); // Vector4 is struct
public static void example(Vector4? color = null) // ? makes it nullable
{
if (color == null)
color = colorBlack;
}