采用C# 2.0,我可以具体说明一种缺省参数价值,如:
static void Test([DefaultParameterValueAttribute(null)] String x) {}
自此C# 4.0 syntax
static void Test(String x = null) {}
因此,在价值类型上是否有C#2.0当量? 例如:
static void Test(int? x = null) {}
以下尝试没有汇编成册。
// error CS1908: The type of the argument to the DefaultValue attribute must match the parameter type
static void Test([DefaultParameterValueAttribute(null)] int? x) {}
// error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression
static void Test([DefaultParameterValueAttribute(new Nullable<int>())] int? x) {}