C# / .NET Compendium PLEN
.NET / C# / WPF

[CallerMemberName] in C#

What it is - an attribute filled in by the compiler

[CallerMemberName] is an attribute from the System.Runtime.CompilerServices namespace that automatically fills a method parameter with the name of the member (property, method) from which that method was called - the compiler does this at compile time, not at runtime, so there is no performance overhead (it is not reflection).

You have already seen it many times, in ViewModelBase:

protected void SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
    if (EqualityComparer<T>.Default.Equals(field, value)) return;
    field = value;
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

The problem it solves

Implementing INotifyPropertyChanged requires passing the property name as a string on every change - which is error-prone and clumsy:

// WITHOUT CallerMemberName - you have to type the name in manually as a string
private string _customerName;
public string CustomerName
{
    get => _customerName;
    set
    {
        _customerName = value;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CustomerName")); // ⚠️ magic string!
    }
}

The problem with the magic string "CustomerName": if someone renames the property through refactoring (Rename in the IDE), the string will NOT change automatically - the compiler won't check it, because it is plain text, not a reference to a symbol. The result: the binding in WPF stops working, and the error only shows up at runtime (the UI simply doesn't update), without any compiler warning.

The solution with [CallerMemberName]

private string _customerName;
public string CustomerName
{
    get => _customerName;
    set => SetProperty(ref _customerName, value); // BEZ podawania nazwy jawnie!
}

protected void SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
    field = value;
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

When the set of the CustomerName property calls SetProperty(ref _customerName, value), the compiler automatically inserts "CustomerName" as the value of the propertyName parameter - exactly as if you had written:

set => SetProperty(ref _customerName, value, "CustomerName"); // this happens IMPLICITLY

The key benefit: if you rename the property with Rename in the IDE (e.g. CustomerName → ClientName), the SetProperty call doesn't need to be touched at all - on the next compilation the compiler itself inserts the new, correct name. A bug like "I forgot to update the string after refactoring" can't happen, because there is no hand-typed string any more.

How exactly it works - "the name of the CALLING member"

public class OrderViewModel : ViewModelBase
{
    private decimal _totalAmount;
    public decimal TotalAmount
    {
        get => _totalAmount;
        set => SetProperty(ref _totalAmount, value); // called INSIDE the TotalAmount setter
    }

    public void RecalculateTotal()
    {
        TotalAmount = 100; // this CALLS the setter, which calls SetProperty with "TotalAmount"
    }

    public void LogSomething([CallerMemberName] string caller = null)
    {
        Console.WriteLine($"Called from: {caller}");
    }
}
vm.RecalculateTotal();
// Inside RecalculateTotal(), if you call LogSomething() directly THERE:
vm.LogSomething(); // prints "RecalculateTotal" - because it was called from THERE

[CallerMemberName] always refers to the direct caller (the method/property in which the call physically sits) - not to the "original" call somewhere higher up the stack.

Syntax requirements

// ✅ Correct - the parameter must have a default value
public void Method([CallerMemberName] string caller = null) { }
public void Method([CallerMemberName] string caller = "") { }

// ❌ Compile error - CallerMemberName requires a default value
public void Method([CallerMemberName] string caller) { }

It must be the last parameter (or at least have a default value, so the method can be called without passing it explicitly) - exactly the same rule as for ordinary optional parameters in C#.

You can still pass a value explicitly - [CallerMemberName] is only the DEFAULT value

protected void SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null) { }

// Normal usage - the compiler inserts it automatically
SetProperty(ref _customerName, value); // propertyName = "CustomerName" (automatycznie)

// You can STILL pass it explicitly when needed
SetProperty(ref _customerName, value, "InnaWlasciwosc"); // propertyName = "InnaWlasciwosc" (jawnie)

This is useful when one property has to notify about a change of another, related property - e.g. when TotalAmount changes and you also want to refresh FormattedTotalAmount in the UI:

private decimal _totalAmount;
public decimal TotalAmount
{
    get => _totalAmount;
    set
    {
        SetProperty(ref _totalAmount, value); // propertyName = "TotalAmount" (automatycznie)
        OnPropertyChanged(nameof(FormattedTotalAmount)); // explicitly - another property has changed
    }
}

public string FormattedTotalAmount => $"{TotalAmount:C}"; // a computed property, no backing field

Related attributes from the same family (System.Runtime.CompilerServices)

Worth mentioning, because they sometimes ask about "the rest of the family":

public void LogError(
    string message,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string filePath = "",
    [CallerLineNumber] int lineNumber = 0)
{
    Console.WriteLine($"[{filePath}:{lineNumber}] {memberName}: {message}");
}
  • [CallerFilePath] - the full path of the source file from which the call was made
  • [CallerLineNumber] - the line number at which the call was made

This is very useful for diagnostic logging - without typing "OrderService.cs" or a line number by hand, the compiler inserts it automatically, and it is always up to date, even after the code has moved.

The alternative - nameof() (for comparison, a different mechanism with a similar goal)

It is worth distinguishing [CallerMemberName] from nameof() - both solve a similar problem (avoiding magic strings), but they work differently:

// nameof() - explicit, at the place of the CALL, any symbol name
OnPropertyChanged(nameof(TotalAmount)); // "TotalAmount" - checked by the compiler, but you have to write it explicitly

// CallerMemberName - implicit, AUTOMATIC, always the name of the CALLER
SetProperty(ref _totalAmount, value); // "TotalAmount" is inserted by itself, because the call is made from HERE

When to use nameof(): when you want to refer to a specific, arbitrary symbol name (it can differ from the one you are currently in) - as in the FormattedTotalAmount example above.

When to use [CallerMemberName]: when you want to automatically know "where I was called from", without forcing the calling code to pass that information explicitly every time.

Summary - why this matters for WPF/MVVM

[CallerMemberName] eliminates magic strings in the INotifyPropertyChanged implementation - instead of typing the property name as text by hand on every SetProperty/OnPropertyChanged (which is fragile under refactoring, because the string is not checked by the compiler nor updated by Rename), the compiler automatically inserts the name of the property/method from which the call was made. It is one of those details that make ViewModelBase safe against refactoring - renaming a property in the IDE will never "silently break" a binding in WPF.

Buy me a coffee