Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Exceptions and Diagnostics

Caller info attributes

Reference for C# caller info attributes: CallerMemberName, CallerFilePath, CallerLineNumber and CallerArgumentExpression, with rules and examples.

Caller info attributes ask the compiler to fill in optional parameters with facts about the call site. You declare a parameter with a default value and put the attribute on it; when a caller leaves the argument out, the compiler inserts the member name, the source file path, the line number or the text of another argument.

The attributes live in System.Runtime.CompilerServices. They are the clean way to write logging helpers, INotifyPropertyChanged code and argument checks without typing strings by hand, which would go stale after a rename.

C#
static void Log(string message, [CallerMemberName] string member = "", [CallerLineNumber] int line = 0)
    => Console.WriteLine($"{member}:{line} {message}");

Try it

C# Member name and argument expression

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# File name and line number

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
[CallerMemberName]                          string member = "";
[CallerFilePath]                            string path   = "";
[CallerLineNumber]                          int    line   = 0;
[CallerArgumentExpression("parameterName")] string? text  = null;   // C# 10
Attribute Value inserted by the compiler
CallerMemberName Name of the calling method, property, event or constructor. In top-level statements the compiler-generated name is <Main>$.
CallerFilePath Full path of the source file at compile time.
CallerLineNumber Line of the call in that file (1-based).
CallerArgumentExpression The source text of the argument passed to another parameter, exactly as written.

Returns: these are attributes; they change the argument the compiler passes, not the return value.

Errors: using an attribute on a parameter without a default value, or on a parameter of an incompatible type, is a compile-time error.

Usage notes

  • The parameter must be optional. The default value is used only when the caller is not the compiler, for example when you call the method through reflection or a delegate.
  • Explicit arguments win. If a caller passes the value, nothing is replaced, which is handy in tests.
  • Values are baked in at compile time. CallerFilePath is the path on the build machine, and deterministic builds can rewrite it with path maps. Do not treat it as a runtime location, and print only Path.GetFileName if you need stable output.
  • Property setters report the property name, which makes SetProperty(ref field, value) helpers for INotifyPropertyChanged short. Use nameof when you need the name outside the member.
  • Local functions and lambdas report the name the compiler gives the enclosing member, which can look odd for lambdas. Prefer named methods for logging.
  • CallerArgumentExpression powers ArgumentNullException.ThrowIfNull(arg), whose message includes the argument text. The text is the source, including spaces: ThrowIfNull(person.Name) reports person.Name.
  • Cost: none at run time; the compiler just passes constants.