是否使用 "Optional, DefaultParameterValue" 属性?
Use "Optional, DefaultParameterValue" attribute, or not?
使用 Optional
和 DefaultParameterValue
属性与不使用它们有什么区别吗?
public void Test1([Optional, DefaultParameterValue("param1")] string p1, [Optional, DefaultParameterValue("param2")] string p2)
{
}
public void Test2(string p1= "param1", string p2= "param2")
{
}
两者都有效:
Test1(p2: "aaa");
Test2(p2: "aaa");
它们的编译方式相同,而且编译器都可以正常工作。唯一的区别是缺少using System.Runtime.InteropServices;
,代码更易读。
作为参考,IL 为:
.method public hidebysig instance void TheName([opt] string p1,
[opt] string p2) cil managed
{
.param [1] = string('param1')
.param [2] = string('param2')
.maxstack 8
L_0000: ret
}
其中 TheName
是唯一发生变化的地方。
namespace System.Runtime.InteropServices {
using System;
//
// The DefaultParameterValueAttribute is used in C# to set
// the default value for parameters when calling methods
// from other languages. This is particularly useful for
// methods defined in COM interop interfaces.
//
[AttributeUsageAttribute(AttributeTargets.Parameter)]
public sealed class DefaultParameterValueAttribute : System.Attribute
{
public DefaultParameterValueAttribute(object value)
{
this.value = value;
}
public object Value { get { return this.value; } }
private object value;
}
}
他们在做同样的工作。您可以在 Roslyn or in ReferenceSource
中查看此类内容
不同之处在于,通过显式使用属性,编译器不会对类型要求强制执行相同的严格性。
public class C {
// accepted
public void f([Optional, DefaultParameterValue(1)] object i) { }
// error CS1763: 'i' is of type 'object'. A default parameter value of a reference type other than string can only be initialized with null
//public void g(object i = 1) { }
// works, calls f(1)
public void h() { f(); }
}
请注意,即使使用 DefaultParameterValue
,您也不会放弃类型安全:如果类型不兼容,这仍将被标记。
public class C {
// error CS1908: The type of the argument to the DefaultParameterValue attribute must match the parameter type
//public void f([Optional, DefaultParameterValue("abc")] int i) { }
}
使用 Optional
和 DefaultParameterValue
属性与不使用它们有什么区别吗?
public void Test1([Optional, DefaultParameterValue("param1")] string p1, [Optional, DefaultParameterValue("param2")] string p2)
{
}
public void Test2(string p1= "param1", string p2= "param2")
{
}
两者都有效:
Test1(p2: "aaa");
Test2(p2: "aaa");
它们的编译方式相同,而且编译器都可以正常工作。唯一的区别是缺少using System.Runtime.InteropServices;
,代码更易读。
作为参考,IL 为:
.method public hidebysig instance void TheName([opt] string p1,
[opt] string p2) cil managed
{
.param [1] = string('param1')
.param [2] = string('param2')
.maxstack 8
L_0000: ret
}
其中 TheName
是唯一发生变化的地方。
namespace System.Runtime.InteropServices {
using System;
//
// The DefaultParameterValueAttribute is used in C# to set
// the default value for parameters when calling methods
// from other languages. This is particularly useful for
// methods defined in COM interop interfaces.
//
[AttributeUsageAttribute(AttributeTargets.Parameter)]
public sealed class DefaultParameterValueAttribute : System.Attribute
{
public DefaultParameterValueAttribute(object value)
{
this.value = value;
}
public object Value { get { return this.value; } }
private object value;
}
}
他们在做同样的工作。您可以在 Roslyn or in ReferenceSource
中查看此类内容不同之处在于,通过显式使用属性,编译器不会对类型要求强制执行相同的严格性。
public class C {
// accepted
public void f([Optional, DefaultParameterValue(1)] object i) { }
// error CS1763: 'i' is of type 'object'. A default parameter value of a reference type other than string can only be initialized with null
//public void g(object i = 1) { }
// works, calls f(1)
public void h() { f(); }
}
请注意,即使使用 DefaultParameterValue
,您也不会放弃类型安全:如果类型不兼容,这仍将被标记。
public class C {
// error CS1908: The type of the argument to the DefaultParameterValue attribute must match the parameter type
//public void f([Optional, DefaultParameterValue("abc")] int i) { }
}