In the Property Setter, this is only useful for setting if the Differs?
I am wondering what advantages this code has:
private int _TestID;
public int TestID
{
get
{
return _TestID;
}
set
{
if(_TestID != value)
{
_TestID = value;
}
}
}
against. this is:
private int _TestID;
public int TestID
{
get
{
return _TestID;
}
set
{
_TestID = value;
}
}
It seems to me that this was done in the name of efficiency (only the setup if different), but wouldn't the test take so long (or longer) that the initial set? I'm not sure if I missed something here, but would love to hear any comments and explanations.
source to share
This is useful when you combine it with RaisePropertyChanged ("TestID") (inside an if, after setting the field value) which is often found with WPF or other data binding solutions.
class Something : INotifyPropertyChanged
{
public int TestID
{
get { return testId; }
set
{
if (testId != value)
{
testId = value;
RaisePropertyChangedEvent("TestID");
}
}
}
}
source to share
I think the example code you gave is not entirely correct. Did you mean it?
private int _TestID;
public int TestID
{
get
{
return _TestID;
}
set
{
if (_TestID != value)
{
_TestID = value;
}
}
}
The reason for this design is also incomprehensible to me. If, however, _TestID is a property instead of an int , then this construct can be useful, since setting _TestID in this case can be an expensive operation.
source to share