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.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

2,918 Spam Comments Blocked so far by Spam Free Wordpress

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>