0Pricing
C# Academy · Lesson

Source-level info (Caller attributes)

Use CallerMemberName, CallerFilePath, and CallerLineNumber to capture call-site info for logs, guards, and property notifications.

Source-level info (Caller attributes) is a free C# Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why caller info

Aim: Capture call-site info automatically.

  • CallerMemberName — who called
  • CallerFilePath — which file
  • CallerLineNumber — which line
  • Great for logs, guards, and notifications

Member name for logs

CallerMemberName auto-fills the caller's method/property name when the argument is omitted.

using System;
using System.Runtime.CompilerServices;

public static class Log
{
  // Caller fills "member" automatically if not provided
  public static void Info(string msg, [CallerMemberName] string member = null)
  {
    Console.WriteLine("[" + (member ?? "?") + "] " + msg);
  }
}

public class Program
{
  static void DoWork() { Log.Info("started"); }

  public static void Main(string[] args)
  {
    Log.Info("app boot");
    DoWork(); // prints caller method name
  }
}

File + line info

Add CallerFilePath and CallerLineNumber for pinpoint diagnostics.

using System;
using System.Runtime.CompilerServices;

public static class Diag
{
  public static void Trace(string msg,
    [CallerFilePath] string file = null,
    [CallerLineNumber] int line = 0,
    [CallerMemberName] string member = null)
  {
    Console.WriteLine(file + ":" + line + " in " + member + " -> " + msg);
  }
}

public class Program
{
  static void Step() { Diag.Trace("stepping"); }

  public static void Main(string[] args)
  {
    Diag.Trace("start");
    Step();
  }
}

Property notifications

Use CallerMemberName in setters or event helpers to avoid hard-coded strings.

using System;
using System.Runtime.CompilerServices;

public class Person
{
  private string _name;

  // Notify helper uses CallerMemberName so callers need not pass the property name
  void Notify([CallerMemberName] string property = null)
  {
    Console.WriteLine("Changed: " + property);
  }

  public string Name
  {
    get { return _name; }
    set { _name = value; Notify(); } // property name auto-filled
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Person p = new Person();
    p.Name = "Ada"; // prints "Changed: Name"
  }
}

Guards with caller name

Caller info makes guard messages clearer without manual strings.

using System;
using System.Runtime.CompilerServices;

public static class Guard
{
  public static void NotNull(object x, [CallerMemberName] string caller = null)
  {
    if (x == null) throw new ArgumentNullException("value", "Null passed by " + (caller ?? "?"));
  }
}

public class Program
{
  static void Save(string path)
  {
    Guard.NotNull(path); // if null, message includes "Save"
    Console.WriteLine("Saving to " + path);
  }

  public static void Main(string[] args)
  {
    try
    {
      Save(null);
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex.Message);
    }
  }
}

Tips & notes

Notes:

  • Caller attributes work on optional parameters with default values.
  • Do not rely on exact file paths for secrets; paths can reveal folders.
  • Great for logging, diagnostics, and property change events.

Caller name attribute

Quick check: Which attribute lets a method get the name of its caller as a default parameter value?

Recap

Recap: Add CallerMemberName, CallerFilePath, and CallerLineNumber to optional parameters to capture caller context for free.

Frequently asked questions

Is the “Source-level info (Caller attributes)” lesson free?

Yes — the full text of “Source-level info (Caller attributes)” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Source-level info (Caller attributes)”?

Use CallerMemberName, CallerFilePath, and CallerLineNumber to capture call-site info for logs, guards, and property notifications. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Source-level info (Caller attributes)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Type, MethodInfo, activation, custom attributes
  2. Source-level info (Caller attributes)
  3. Light metaprogramming scenarios
← Back to C# Academy