Uredi

Deli z drugimi prek


DefaultParameterValueAttribute Class

Definition

Sets the default value of a parameter when called from a language that supports default parameters. This class cannot be inherited.

public ref class DefaultParameterValueAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Parameter)]
public sealed class DefaultParameterValueAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Parameter)>]
type DefaultParameterValueAttribute = class
    inherit Attribute
Public NotInheritable Class DefaultParameterValueAttribute
Inherits Attribute
Inheritance
DefaultParameterValueAttribute
Attributes

Examples

The following code example demonstrates how to apply the DefaultParameterValueAttribute attribute to a parameter of a method written in C#. The snippet also uses the OptionalAttribute attribute 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
    }    
}

There are some behavior differences between C# optional arguments and using DefaultParameterValueAttribute with OptionalAttribute. The following code snippet demonstrates these differences.

using System.Runtime.InteropServices;

public class Program
{
    public static void MethodWithObjectDefaultAttr1([Optional, DefaultParameterValue(123)] object obj) {} // OK
    public static void MethodWithObjectDefaultAttr2([Optional, DefaultParameterValue("abc")] object obj) {} // OK
    public static void MethodWithObjectDefaultAttr3([Optional, DefaultParameterValue(null)] object? obj) {} // OK

    public static void MethodWithObjectDefaultParam1(object obj = 123) {} // CS1763
    public static void MethodWithObjectDefaultParam2(object obj = "abc") {} // CS1763
    public static void MethodWithObjectDefaultParam3(object obj? = null) {} // OK
}

Remarks

The DefaultParameterValueAttribute attribute allows you to specify a default parameter value in a language that does not otherwise support default parameters. After you apply this attribute to your code, languages that support default parameters can use the specified value as a default parameter.

The DefaultParameterValueAttribute is particularly useful to specify default parameters for methods of a COM interop interface.

Constructors

DefaultParameterValueAttribute(Object)

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

Properties

TypeId

When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)
Value

Gets the default value of a parameter.

Methods

Equals(Object)

Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode()

Returns the hash code for this instance.

(Inherited from Attribute)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute()

When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object)

When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

Applies to