Olvasás angol nyelven Szerkesztés

Megosztás a következőn keresztül:


DefaultParameterValueAttribute(Object) Constructor

Definition

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

C#
public DefaultParameterValueAttribute(object value);
C#
public DefaultParameterValueAttribute(object? value);

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.

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

Termék Verziók
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0