In the provided example, the function declaration PrintBar is throwing an error because _bar is not a compile time constant. What is the best practice to avoid this issue?
using System; class Foo { private readonly string _bar; public Foo(string initBar) { _bar = initBar; } public void PrintBar(string value = _bar) { Console.WriteLine(value); } }
nullbe the default? And replace it with_barifnullwas passed._barto be used as default I'd usenullas formal default:void PrintBar(string value = null)and in the method's body replace null by_bar._bar. Instead of overloading, give them different names entirely. I recognize that this might be a trivial example, but the same line of reasoning should be used to inform your design decisions in real-world scenarios, as well.