Why do the C# Caller Info Attributes need a default value?

Those parameters need a default value because the Caller Info attributes were implemented using optional parameters and optional parameters require a default value. That way the call can be simply ShowCallerInfo() without having to send any parameters and the compiler will add the relevant ones.

Why was it implemented using optional parameters to begin with is a deeper question. They could have made it without, and the compiler would need to "inject" those parameters before actual compilation started, but as opposed to optional parameters (which is a C# 4.0 feature) it would not be backward compatible and it will break other compilers/code analysis tools.


They need the defaults so that the parameters can be flagged as optional. If you don't specify the parameters when calling the method, the compiler will inject the correct values for you, but only if you didn't specify them. If you do, then the "magic" of those attributes won't happen.

From my understanding, these attributes do not affect runtime and are purely for compile time so the defaults are only to make sure the parameters are optional.


To put it in another way, on the callee (the method called where the attribute is applied to the parameter) the parameter must exist. On the other hand, the caller must pass those arguments and the only way for the compiler to allow for an unspecified argument is to give it a default value.

Although attributes may influence code generation or runtime execution, the source must be valid if one removes all the attributes. Therefor the default value must be defined on the callee and the compiler just generate the argument value based on the applied attribute instead of the current default value defined on the callee.