Bookmark and Share

Property shortcuts in ASP.NET C#

Posted on Sun 14 June 2009

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.


Permalink | Comments (0)
Tags: , . Categories: ASP.NET | C#.

Comments

Add a comment

Name, e-mail and your comments are all required fields

biuquote
Loading



< go back to the main blog page

< go up to the top of the page