class Program { class ReactiveVariable<T> { Func<T> function; public ReactiveVariable(Func<T> function) { this.function = function; } public static implicit operator T(ReactiveVariable<T> rv){ return rv.function(); } public override string ToString() { return this.function().ToString(); } } static void Main(string[] args) { var x = 5; var y = 10; var z = new ReactiveVariable<int>(() => x * y); Console.WriteLine(z); // z == 50 x = 2; Console.WriteLine(z); // z == 20 y = 5; Console.WriteLine(z); // z == 10 // Implicit conversions work too. int i = z; Console.WriteLine(i); }}
Edit: Once converted to a regular int you do of course lose the magic of the function call, so I'd be tempted to stick with the function call or at least make the conversion operator explicit.
[Edited by - benryves on July 2, 2010 9:52:24 AM]