我可以省略现场创作,如果我需要定制的getter / setter? [英] can I omit field creation if I need custom getter/setter?
问题描述
我可以写这样的beatiful简单的代码:
公众诠释三角洲{搞定;私人集; }
现在我想补充只需一个电话 OnPropertyChanged(达美) ;
这是我知道如何做到这一点的唯一方法:
公众诠释三角洲{{返回_delta; }私定
{
_delta =价值; OnPropertyChanged(达美); }
}
私人诠释_delta;
这是太多额外的代码!我为什么要在这种情况下,引进外地?你可以让这段代码更短?我想有这样的事情,但它不工作:
公众诠释三角洲{搞定;私人集合{OnPropertyChanged(达美); }}
没有你不能用自动性能做到这一点。自动性质,目的是要为在一个场的属性短手语法。它们几乎没有任何其他能力。任何自定义代码的执行需要一个完全成熟的属性。
I can write such beatiful straightforward code:
public int Delta { get; private set; }
Now I want to add just one call OnPropertyChanged("Delta");
This is the only way I know how to do that:
public int Delta { get { return _delta; } private set
{
_delta = value; OnPropertyChanged("Delta"); }
}
private int _delta;
It's too much extra-code! Why should I introduce field in this case? Can you make this code shorter? I would like to have something like that, but it doesn't work:
public int Delta { get; private set { OnPropertyChanged("Delta"); } }
No you cannot do this with auto-properties. Auto-properties are meant to be short hand syntax for a property over a field. They have virtually no other capabilities. Any custom code execution requires a full fledged property.
这篇关于我可以省略现场创作,如果我需要定制的getter / setter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!