DefaultParameterValueAttribute(Object) Konstruktor

Definicja

Inicjuje nowe wystąpienie DefaultParameterValueAttribute klasy z wartością domyślną parametru.

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)

Parametry

value
Object

Obiekt reprezentujący wartość domyślną parametru.

Przykłady

W poniższym przykładzie kodu pokazano, jak zastosować DefaultParameterValueAttribute atrybut do parametru metody napisanej w języku C#. Atrybut OptionalAttribute jest również używany do umożliwienia wywoływanie metody bez żadnych argumentów.

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

Uwagi

Użyj tego konstruktora, aby zastosować atrybut do parametru DefaultParameterValueAttribute napisanego w języku, takim jak Microsoft Visual C#, który nie obsługuje parametrów domyślnych.

Dotyczy