Caller Information Attributes · TL;DR

1 min read
Mid-level7 min read
Rapid overview

TL;DR

Caller information attributes are applied to optional parameters and tell the compiler to fill in facts about the call site when the caller leaves the argument out: [CallerMemberName] (the calling method or property name), [CallerFilePath] (the source file's full path at compile time), [CallerLineNumber] (the line), all C# 5, and [CallerArgumentExpression("param")] (C# 10, the source text of another argument). The values are constants baked into the caller's IL at compile time, so they cost nothing at runtime and are not affected by inlining, unlike walking the stack with StackTrace. Typical uses: INotifyPropertyChanged without magic strings, logging and tracing helpers, and guard clauses whose error message shows the failing expression (ArgumentNullException.ThrowIfNull(order.Customer) reports order.Customer). Traps: the attributes only work if every wrapper forwards them, CallerFilePath leaks build-machine paths unless you set PathMap, and an explicit argument overrides the compiler's value.