【发布时间】:2014-08-22 12:55:02
【问题描述】:
我正在使用一些现有代码并试图找出在实现 INotifyPropertyChanged 接口时使用字符串常量作为属性名称的优势(如果有的话)。
例如这样做:
/*
* Why use this instead of string literal
* in OnPropertyChanged below??
*/
public const string CustomerIdPropertyName = "CustomerId";
private int _customerId;
public int CustomerId
{
get
{
return _customerId;
}
set
{
if (_cusomterId != value)
{
_customerId = value;
OnPropertyChanged(CustomerIdPropertyName);
}
}
}
而不是这个:
private int _customerId;
public int CustomerId
{
get
{
return _customerId;
}
set
{
if (_cusomterId != value)
{
_customerId = value;
OnPropertyChanged("CustomerId");
}
}
}
【问题讨论】:
-
没有理由使用常量。在功能上它们是相同的。
-
您的 IDE 可以帮助您输入
CustomerIdPropertyName,这是我认为的唯一优势。 -
@user2348184 好吧,他们是否从其他任何地方引用了该名称?也许是
switch(e.PropertyName) {...}或类似的?
标签: c# wpf inotifypropertychanged