Is there a way to write the C# method below:
public string Download(Encoding contentEncoding = null) {
defaultEncoding = contentEncoding ?? Encoding.UTF8;
// codes...
}
with a default parameter added so it looks like this:
public string Download(Encoding contentEncoding = Encoding.UTF8) {
// codes...
}
without using a compile-time constant?
In short. No.
Optional parameters are required to be compile time constants or value types.
From Named and Optional Arguments (C# Programming Guide) on MSDN:
What you seem to want to achieve can be accomplished by overloading:
Note that this is not quite the same as optional parameters, as the default value gets hard coded into the caller with optional parameters (which is why the restrictions for them exist).