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#。

適用於