c # – Is the use of a non-constant value at compile time as a default setting a code smell?


This question already has an answer here:

I am trying to use App.config and Configuration Manager for the first time. I wanted to use some values ​​in App.config as default settings for some methods, but this causes an error (not a compile time constant):

public void ThisDoesntWork (string parameter = ConfigurationManager.AppSettings["SettingName"])
{
// Error: Not a compilation time constant.
}

I understand a little why this is the case, so I found this workaround:

public void ThisWorks (string parameter = "Use App.config")
{
if (parameter == "Use App.config")
{
parameter = ConfigurationManager.AppSettings["SettingName"]
    }
// rest of the method.
}

It should be noted that this could also be used to transform the return value of a static method (or anything that is not a compile time constant) into a default parameter value.

It seems a little strange and I wondered if it was a code smell. Has anyone ever used a solution of this type before and encountered problems? What is the best practice in this situation?