DefaultParameterValueAttribute 类   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
设置当从支持默认参数的语言中调用参数时参数的默认值。 此类不能被继承。
public ref class DefaultParameterValueAttribute sealed : Attribute[System.AttributeUsage(System.AttributeTargets.Parameter)]
public sealed class DefaultParameterValueAttribute : Attribute[<System.AttributeUsage(System.AttributeTargets.Parameter)>]
type DefaultParameterValueAttribute = class
    inherit AttributePublic NotInheritable Class DefaultParameterValueAttribute
Inherits Attribute- 继承
- 属性
示例
下面的代码示例演示如何将 DefaultParameterValueAttribute 特性应用于用 C# 编写的方法的参数。 代码片段还使用 OptionalAttribute 特性,使方法无需任何参数即可调用。
using System;
using System.Runtime.InteropServices;
public class Program
{
    public static void MethodWithDefaultParam([Optional, DefaultParameterValue("DEFAULT_PARAM_VALUE")] string str)
    {
        Console.WriteLine($"The passed value is: {str}");
    }
    public static void Main()
    {
        MethodWithDefaultParam(); // The passed value is: DEFAULT_PARAM_VALUE
        MethodWithDefaultParam("NEW_VALUE"); // The passed value is: NEW_VALUE
    }    
}
C# 可选参数与与 OptionalAttribute结合使用DefaultParameterValueAttribute之间存在一些行为差异。 以下代码片段演示了这些差异。
using System.Runtime.InteropServices;
public class Program
{
    public static void MethodWithObjectDefaultAttr1([Optional, DefaultParameterValue(123)] object obj) {} // OK
    public static void MethodWithObjectDefaultAttr2([Optional, DefaultParameterValue("abc")] object obj) {} // OK
    public static void MethodWithObjectDefaultAttr3([Optional, DefaultParameterValue(null)] object? obj) {} // OK
    public static void MethodWithObjectDefaultParam1(object obj = 123) {} // CS1763
    public static void MethodWithObjectDefaultParam2(object obj = "abc") {} // CS1763
    public static void MethodWithObjectDefaultParam3(object obj? = null) {} // OK
}
注解
属性 DefaultParameterValueAttribute 允许你以不支持默认参数的语言指定默认参数值。 将此属性应用于代码后,支持默认参数的语言可以使用指定的值作为默认参数。
DefaultParameterValueAttribute对于为 COM 互操作接口的方法指定默认参数,特别有用。
构造函数
| DefaultParameterValueAttribute(Object) | 用参数的默认值初始化 DefaultParameterValueAttribute 类的新实例。 | 
属性
| TypeId | 在派生类中实现时,获取此 Attribute 的唯一标识符。(继承自 Attribute) | 
| Value | 获取参数的默认值。 | 
方法
| Equals(Object) | 返回一个值,该值指示此实例是否与指定的对象相等。(继承自 Attribute) | 
| GetHashCode() | 返回此实例的哈希代码。(继承自 Attribute) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| IsDefaultAttribute() | 在派生类中重写时,指示此实例的值是否是派生类的默认值。(继承自 Attribute) | 
| Match(Object) | 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。(继承自 Attribute) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | 将一组名称映射为对应的一组调度标识符。(继承自 Attribute) | 
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) | 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。(继承自 Attribute) | 
| _Attribute.GetTypeInfoCount(UInt32) | 检索对象提供的类型信息接口的数量(0 或 1)。(继承自 Attribute) | 
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | 提供对某一对象公开的属性和方法的访问。(继承自 Attribute) |