skip to content

A C# reporting library's v2 changes `Export`'s default `bufferSize` from 4096 to 81920; why does an app compiled against v1 still pass 4096 after the DLL is upgraded?

level: seniorimportance: must knowfreq 48%

answer

  1. the caller holds the value
  2. a constant in the parameter's metadata
  3. copied when the caller compiles
  4. move the default into the body

basics

~20 s

The C# compiler copies an optional parameter's default into each call site when the caller is compiled, so the app's IL still contains the literal 4096. Only rebuilding the app against v2 picks up 81920.

solid answer

~40 s

An optional parameter is stored in the library's metadata as a parameter flagged `Optional` and `HasDefault`, with its constant value. When the app was compiled, the compiler read that constant and emitted it as an ordinary argument, so `Report.Export("q3.csv")` was compiled as if it were `Report.Export("q3.csv", false, 4096)`. Replacing the DLL changes the method body that runs, not the arguments an old call site pushes. Rebuilding the app against v2 is what changes them. If a library must be able to change a default without its callers recompiling, the default has to live in the method body: declare a sentinel such as `int? bufferSize = null` and resolve `bufferSize ?? DefaultBufferSize` inside, or give callers an overload that supplies the value internally.

code

plaintext · 6 lines
plaintext
--- built against v1, running v1
v1 Export(q3.csv, overwrite: False, bufferSize: 4096)
--- built against v1, running v2_default
v2 Export(q3.csv, overwrite: False, bufferSize: 4096)
--- recompiled against v2_default
v2 Export(q3.csv, overwrite: False, bufferSize: 81920)

go deeper

for a junior

Recall that a default value is filled in where the method is called, when that code is compiled.

for a middle

Explain how a default is stored in metadata and copied into the caller's IL, and why replacing the DLL does not change it.

for a senior

Diagnose a stale default in production from the build history, separate binary from behavioural compatibility, and apply the sentinel pattern.

for a principal

Decide which library parameters may ever carry a plain constant default, given that every published value is frozen into consumers' binaries.

## Where an optional parameter's default lives A reporting library ships as a NuGet package with this method in v1: ```csharp public static string Export(string path, bool overwrite = false, int bufferSize = 4096) ``` The default `4096` is not code that runs inside `Export`. The compiler records it in the library's **metadata**: reflection shows the parameter's attributes as `Optional, HasDefault`, and `ParameterInfo.DefaultValue` returns `4096`. The method body itself always receives three arguments. Other kinds of default, such as an enum member, a string literal or `null`, are recorded the same way, and nothing about them is looked up again when the call runs. When an application calls `Report.Export("q3.csv")`, the C# compiler reads the metadata of the referenced assembly, sees that two arguments are missing, and **emits the default values as constants in the caller's IL**. From the runtime's point of view the application calls `Export("q3.csv", false, 4096)`. This is a compile-time substitution, exactly like a `const` field from another assembly being copied into the code that reads it, a mechanism covered in the lesson on constants. ## What happened in the upgrade Version 2 changes the declaration to `int bufferSize = 81920` and keeps the signature `Export(string, bool, int)`. Three runs with two separately compiled assemblies show the effect: 1. The app built against v1 and run with v1 passes 4096. 2. The **same app binary** run with the v2 DLL copied over still passes 4096. The v2 body runs, but the argument was fixed in the app. 3. The app **rebuilt** against v2 passes 81920. The change is **binary compatible**: nothing crashes, because the method with that exact signature still exists. It is also **behaviour breaking**: old binaries silently keep the old default. The C# versioning guide puts it this way: callers that do not supply those arguments need to be updated when a default changes. Adding a parameter is a different failure. If v2 instead appends `bool compress = false`, the signature becomes `Export(string, bool, int, bool)`, the old one is gone, and the v1-compiled app throws `MissingMethodException`. In a test run it was thrown when the runtime compiled the method containing the call, before that method's first line ran. That design problem belongs to the question on evolving a published API. ## Fixes, from best to worst - **Make the default a sentinel and resolve it in the body.** Declare `int? bufferSize = null` and compute `int size = bufferSize ?? DefaultBufferSize;` inside `Export`. The caller only ever bakes in `null`, and the library decides what `null` means in every version. - **Use an overload instead of a default.** `Export(string path)` calls `Export(path, false, DefaultBufferSize)` from inside the library, so the value lives in library code. - **Treat a changed default as a breaking change.** If the parameter must stay a plain constant, document the change and require consumers to rebuild. - **Rebuild the app.** For one application this is the immediate fix, but it does nothing for other consumers of the package. A `public const int DefaultBufferSize = 81920;` used as the default does **not** help: the const value is copied into the call site too. A `static readonly` field cannot be used as a default at all, because it is not a compile-time constant (CS1736). ## Diagnosing it in production - Confirm which package version the application was **compiled** against, not which DLL is deployed next to it. - Inspect the caller's IL with an IL viewer: the literal argument sits in the calling method, not in the library. - Log the effective value inside the library, such as the resolved buffer size, so a mismatch shows up in telemetry instead of in performance graphs. - Check for other callers compiled at different times, such as plugins, that may each carry a different baked-in value. ## What interviewers listen for The signal is the phrase **"resolved at the call site at compile time"** and the consequence that a library author cannot change the effective default for existing binaries. A strong answer separates binary compatibility (it still runs) from behavioural compatibility (it still does what v2 intends) and proposes the sentinel pattern without prompting. Adding that `const` values behave the same way, and that appending a parameter fails differently, with an exception instead of a stale value, shows the whole mechanism is understood.

  • Why does appending bool compress = false to Export crash the old app instead of silently using the new default?
    The old call site names the method by its exact signature, `Export(string, bool, int)`. After the change only `Export(string, bool, int, bool)` exists, so the runtime throws `MissingMethodException: Method not found: 'System.String Reporting.Report.Export(System.String, Boolean, Int32)'`. The new default exists only for callers compiled against v2.
  • What does the int? bufferSize = null sentinel cost compared with a plain default?
    The method must handle `null` explicitly, callers can pass `null` on purpose and get the library default, and the documentation has to say so. It also changes the parameter type, which is itself a breaking change for compiled callers, so introduce it in a major version or through a new overload.
  • Which changes to an existing optional parameter are safe for already-compiled callers?
    Changing the method body is safe. Making a required parameter optional keeps the signature, so compiled callers run unchanged. Changing a default value runs but keeps the old value in old binaries. Changing the parameter's type, or adding or removing a parameter, changes the signature and breaks them.

  • The runtime reads the default from the library each time the method is called.
  • Changing a default value is picked up by every app as soon as the DLL is replaced.
  • Using a public const as the default lets the library change it later.
  • Appending an optional parameter is binary compatible with old callers.