System.Runtime.CompilerServices.CallerMemberNameAttribute Class

Allows you to obtain the method or property name of the caller to the method.

See Also: CallerMemberNameAttribute Members


[System.AttributeUsage(System.AttributeTargets.Parameter, Inherited=false)]
public sealed class CallerMemberNameAttribute : Attribute


You apply the CallerMemberName attribute to an optional parameter that has a default value. You must specify an explicit default value for the optional parameter. You can't apply this attribute to parameters that aren't specified as optional.

For more information, see Caller Information (C# and Visual Basic).

You can use the CallerMemberName attribute to avoid specifying the member name as a String argument to the called method. By using this technique, you avoid the problem that Rename Refactoring doesn't change the String values. This is especially useful for the following tasks:

The following chart shows the member names that are returned when you use the CallerMemberName attribute.

Call occurs within

Member name result

Method, property, or event

The name of the method, property, or event from which the call originated.


The string ".ctor"

Static constructor

The string ".cctor"


The string "Finalize"

User-defined operators or conversions

The generated name for the member, for example, "op_Addition".

Attribute constructor

The name of the member to which the attribute is applied. If the attribute is any element within a member (such as a parameter, a return value, or a generic type parameter), this result is the name of the member that's associated with that element.

No containing member (for example, assembly-level or attributes applied to types)

The default value of the optional parameter.


Namespace: System.Runtime.CompilerServices
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: