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.
static void Log(string message, [CallerMemberName] string member = "", [CallerLineNumber] int line = 0) => Console.WriteLine($"{member}:{line} {message}");
Try it
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.
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
[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.
CallerFilePathis 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 onlyPath.GetFileNameif you need stable output. - Property setters report the property name, which makes
SetProperty(ref field, value)helpers forINotifyPropertyChangedshort. Usenameofwhen 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.
CallerArgumentExpressionpowersArgumentNullException.ThrowIfNull(arg), whose message includes the argument text. The text is the source, including spaces:ThrowIfNull(person.Name)reportsperson.Name.- Cost: none at run time; the compiler just passes constants.