void Main() { // Settings are passed by value when each worker is // instantiated. I want to pass the constructor // something that allows each setting to be read // dynamically whenever it is used. mWorker worker1 = new mWorker("W1", () => Settings.setting1); mWorker worker2 = new mWorker("W2", () => Settings.setting2); Console.WriteLine("\n====================="); worker1.doWork(); // Result: "Worker W1 is turned ON" worker2.doWork(); // Result: "Worker W2 is turned OFF" Settings.setting1 = false; Settings.setting2 = true; Console.WriteLine("\n====================="); worker1.doWork(); // Result: "Worker W1 is turned OFF" worker2.doWork(); // Result: "Worker W2 is turned ON" }
static void Main(string[] args) { var settingsForWorker1 = new Settings() { MySetting = true }; mWorker worker1 = new mWorker("W1", settingsForWorker1); var settingsForWorker2 = new Settings() { MySetting = false }; mWorker worker2 = new mWorker("W2", settingsForWorker2); Console.WriteLine("\n====================="); worker1.doWork(); // Result: "Worker W1 is turned ON" worker2.doWork(); // Result: "Worker W2 is turned OFF" settingsForWorker1.MySetting = false; settingsForWorker2.MySetting = true; Console.WriteLine("\n====================="); worker1.doWork(); // Desired result: "Worker W1 is turned OFF" worker2.doWork(); // Desired result: "Worker W2 is turned ON" Console.ReadLine(); }