I am a little confused about how much I SHOULD do with properties.
I have heard that properties should always represent a logical property of the class.
Get and Set should almost never throw exceptions with the exception of ArgumentOutOfRange. Is that true? Is the following example totally wrong?
public bool DeviceRegistered
{
get{ return _Registered;}
set
{
if(value)
{
RegisterDevice();
_Registered = true;
}
else
{
UnRegisterDevice();
_Registered = false;
}
}
}
Also, If a method in the same class wants to change the value of a property should it go through the set accessor of the property or just modify the private variable _Registered directly?
If you have any additional advice when using properties please include!
Thanks
Here is a link to the Design Guidelines for properties from the MSDN. Make special note of the Property vs Method section.
From my own personal experience, you shouldn’t use properties to do a lot of work. They should be returning information that has already been retrieved. I’m currently working on a code base which has a lot of lazy loaded properties that retrieve information from a web service. Looking at the properties of a class while debugging causes all of the properties to be evaluated, causing functional evaluation to time out and the ASP.NET process to crash.