a DotNetStyling - .Net World by Armen Ayvazyan : C# 3.0 Features. Part I: Auto Property
Welcome to DotNetStyling Sign in | Join | Help
Add to Technorati Favorites Add to Google Reader or Homepage Add to My AOL Subscribe in FeedLounge Subscribe in Bloglines Add to Excite MIX Add to flurry Add to Pageflakes Subscribe in NewsGator Online


C# 3.0 Features. Part I: Auto Property

 

I would like to continue my blogging experience with a series of blog posts related to C# 3.0 features. Although there are already a lot of materials regarding this subject in .Net community my impression is so strong that it forces me write about it.

I decided to post simple blog per feature. The first one in the list is “Auto Property”

 

Redundancy in Property Declaration

Usually, we use properties as data carrier which do not contain any business logic inside. This especially visible on DTO class implementation. For every property in the class we have to introduce private field which connects to property in getter and/or setter without being used in other parts of the class.

   1: public class User
   2: {
   3:     private int _userId;
   4:     public int UserId
   5:     {
   6:         get { return _userId; }
   7:         set { _userId = value; }
   8:     }
   9:  
  10:     private string _userName;
  11:     public string UserName
  12:     {
  13:         get { return _userName; }
  14:         set { _userName = value; }
  15:     }
  16:  
  17: }

As you can see we face to redundancy in property declaration.

 

Simplify Property Declaration

By using Auto Property feature of C# 3.0 we can declare a property without private field declaration, with empty setter and getter (using the same style as we do in interfaces). The compiler will create an appropriate code for us. It will create a private field and use it in set_ get_ methods accordingly.

On the example below the UserId property has been converted to Auto Property declaration style where UserName remained the same.

   1: public class User
   2: {
   3:     public int UserId { get; set; }
   4:  
   5:     private string _userName;
   6:     public string UserName
   7:     {
   8:         get { return _userName; }
   9:         set { _userName = value; }
  10:     }
  11: }

 

We will compile the code and explore the result. IL Disassembler shows that compiler created both "get_XXX" and "set_XXX" methods for UserId property the same way as it did for UserName.

image

 

 

Advantage

The advantage of this feature is obvious:

  • Saves time during class declaration
  • Removes redundancy in the code
  • Code becomes much more clear and readable
   1: public class User
   2: {
   3:     public int UserId { get; set; }
   4:     public string UserName { get; set; }
   5: }

 

BTW, we still can play with access modifiers of getter and setter if needed:

   1: public class User
   2: {
   3:     public int UserId { get; private set; }
   4:     public string UserName { get; internal set; }
   5: }

 

Attention

Auto property applies only on public properties which private member is not used in other part of the class (including constructor).

 

Technorati tags: ,

Share this post :

Posted: 31. července 2008 11:47 by Armen Ayvazyan
Filed under: ,

Comments

New Comments to this post are disabled