¿Puedo crear una propiedad automática (sin miembro privado) con el código get y set?
En C #, puedo hacer esto:
public int Foo { get; set; }
Lo cual es bueno. Pero tan pronto como quiera hacer algo en el getter o setter, tengo que cambiarlo a esto:
private int foo;
public int Foo {
get { return foo; }
set {
foo = value;
DoSomething(); // all that other code, just to add this line!
}
}
¿Es posible evitar esto? Me encantaría poder hacer algo como esto:
public int Foo {
get;
set {
DoSomething();
}
}
¿Qué tan cerca puedo llegar a lo anterior?