DefaultParameterValueAttribute(Object) 构造函数

定义

用参数的默认值初始化 DefaultParameterValueAttribute 类的新实例。

public:
 DefaultParameterValueAttribute(System::Object ^ value);
public DefaultParameterValueAttribute (object value);
public DefaultParameterValueAttribute (object? value);
new System.Runtime.InteropServices.DefaultParameterValueAttribute : obj -> System.Runtime.InteropServices.DefaultParameterValueAttribute
Public Sub New (value As Object)

参数

value
Object

表示参数默认值的对象。

示例

下面的代码示例演示如何将 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
    }    
}

注解

使用此构造函数将 DefaultParameterValueAttribute 特性应用于以不支持默认参数的语言(如 Microsoft Visual C#)编写的参数。

适用于