แก้ไข

แชร์ผ่าน


DefaultParameterValueAttribute(Object) Constructor

Definition

Initializes a new instance of the DefaultParameterValueAttribute class with the default value of a parameter.

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)

Parameters

value
Object

An object that represents the default value of a parameter.

Examples

The following code example demonstrates how to apply the DefaultParameterValueAttribute attribute to a parameter of a method written in C#. The OptionalAttribute attribute is also used to enable the method to be called without any arguments.

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
    }    
}

Remarks

Use this constructor to apply the DefaultParameterValueAttribute attribute to a parameter written in a language such as Microsoft Visual C# that does not support default parameters.

Applies to