Best Way
-
Hi, I would like to describe two way of declaring properties (1)
public string Get_Set { get { return get_set; } set { get_set=value; } }
(2)public string Get_Set { get { return get_set; } set { if(get_set!=value) get_set=value; } }
I would like to clear few things in property declaration. a) From the above two property declaration , which one is best while considering efficient resource utilization. b) If the above given examples are not good enough , then which one is best way of declaring a property. Comment with detailed description will be highly appreciable. Sreejith Nair [ My Articles ] -
Hi, I would like to describe two way of declaring properties (1)
public string Get_Set { get { return get_set; } set { get_set=value; } }
(2)public string Get_Set { get { return get_set; } set { if(get_set!=value) get_set=value; } }
I would like to clear few things in property declaration. a) From the above two property declaration , which one is best while considering efficient resource utilization. b) If the above given examples are not good enough , then which one is best way of declaring a property. Comment with detailed description will be highly appreciable. Sreejith Nair [ My Articles ]Hello, For simplicity, if it's only setting a variable and not doing something else, I'd use the first case:
public string MyProperty { get { return myProperty; } set { myProperty = value; } }
If setting the property implies other operations, then, depending on them, I would check the previous value. For example, if when a property is set to
false
I need to remove an event handler, I would check before that the property was not alreadyfalse
, so I don't remove the handler twice. But for simple properties that only set variables, no need to check. -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005