Was trying to implement generic property, how did I fail?

class Program { static void Main(string[] args) { Person p1 = new Person(); p1.Age = 29; p1.Age = 28; p1.Age.Undo(); Console.WriteLine(p1.Age); } } class Person { public BusinessProperty<string> Name { get; set; } public BusinessProperty<int> Age { get; set; } } public class BusinessProperty<T> { private Stack<T> changes = new Stack<T>(); public T Value { get { // insert desired logic here return changes.LastOrDefault(); } set { // insert desired logic here changes.Push(value); } } public static implicit operator T(BusinessProperty<T> value) { return value.Value; } public static implicit operator BusinessProperty<T>(T value) { return new BusinessProperty<T> { Value = value }; } public T Undo() { try { return changes.Pop(); } catch (InvalidOperationException e) { Console.WriteLine(e.Message); } return changes.LastOrDefault(); } } 

why doesn't changes persist?

Constructor of BusinessProperty seems to fire every time I assign new value to same property, that's not what I want.

Any way to fix it?

by Karmadilla via /r/csharp

Leave a Reply