Property shortcuts in ASP.NET C#
This is a useful time saving technique for ASP.NET C# 3.0 that allows any future re-implementation of the properties.
In C# 2.0 (and often still in C# 3.0), properties of a class are defined as a private variable and encapsulated within a public getter/setter as follows. The thing is with this though is that it does nothing more than put a public wrapper round the private variable, which adds no real value (you almost might as well just define public variables in the first place, which is bad).
private bool isReady;
public bool IsReady
{
get { return isReady; }
set { isReady = value;}
}
A better and quicker implementation is to use property shortcuts. The code above could be rewritten as:
public bool IsReady { get; set; }
With this way of doing things, there is no need to define a private variable, as that is essentially what .NET does for you behind the scenes. If you do want to change the internal implementation at a later date, you can and you it won’t affect the external use, much like when using an interface.
-
Calendar
February 2012 M T W T F S S « Feb 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 -
Meta





