c# - which form of property declaration is more appropriate? -
it's been while simple question disturbing me , i've seen 2 form of declaring properties , , wondering what's different , there reason makes me prefer 1 ?
1st form :
public int age { get; set; }
2nd form :
private int _age; public int age { { return _age; } set { _age = value; } }
i've used second form , i've seen msdn tutorial talks 2nd form , i'm little confused here , 1 preferred ?
the first form "new". find lot of "old" code contains second form.
today, using modern versions of c#, reason use second form hook more logic inside getter or setter.
for instance:
private int mypropertyvalue; public int myproperty { { return mypropertyvalue; } set { if (value >= 0 && value <= 100) { mypropertyvalue = value; } else { // throw exception here. } } }
Comments
Post a Comment