Geeks With Blogs

News



Microsoft Store

Support This Site


AddThis Social Bookmark Button

Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

Creative Commons License


Scott Dorman Microsoft MVP, Software Architect, Developer, Author

Defining properties can be very tedious. Suppose you have a class

   1: public class UserProfile
   2: {
   3:     private int userrId;
   4:     private string firstName;
   5:     private string lastName;
   6:  
   7:     public int UserId
   8:     {
   9:         get { return this.userId; }
  10:         set { this.userId = value; }
  11:     }
  12:  
  13:     public string FirstName
  14:     {
  15:         get { return this.firstName; }
  16:         set { this.firstName = value; }
  17:     }
  18:     
  19:     public string LastName
  20:     {
  21:         get { return this.lastName; }
  22:         set { this.lastName = value; }
  23:     }
  24:  
  25:     public UserProfile() { }   
  26: }

It might not look like a lot of code, but when you think about the steps you need to go through to write that small amount of code, it can add up.

  1. Decide on the public property name.
  2. Declare the appropriately named and typed backing variable.
  3. Implement the corresponding public property.

Fortunately, C# 3.0 provides a way to simplify this. Now, we can write the class this way:

   1: public class UserProfile
   2: {
   3:     public int UserId { get; set; }
   4:     public string FirstName { get; set; }
   5:     public string LastName { get; set; }
   6:  
   7:     public UserProfile() { }
   8: }

This is certainly much more compact and requires fewer steps. When the compiler sees this class, (according to Reflector) it translates it to:

   1: public class UserProfile
   2: {
   3:     [CompilerGenerated]
   4:     private int <UserId>k_BackingField;
   5:     [CompilerGenerated]
   6:     private string <FirstName>k_BackingField;
   7:     [CompilerGenerated]
   8:     private string <LastName>k_BackingField;
   9:     
  10:     public int UserId
  11:     {
  12:         [CompilerGenerated]
  13:         get
  14:         {
  15:             return this.<UserId>k_BackingField;
  16:         }
  17:         [CompilerGenerated]
  18:         set
  19:         {
  20:             this.<UserId>k_BackingField = value;
  21:         }
  22:     }
  23:  
  24:     public string FirstName
  25:     {
  26:         [CompilerGenerated]
  27:         get
  28:         {
  29:             return this.<FirstName>k_BackingField;
  30:         }
  31:         [CompilerGenerated]
  32:         set
  33:         {
  34:             this.<FirstName>k_BackingField = value;
  35:         }
  36:     }
  37:             
  38:     public int LastName
  39:     {
  40:         [CompilerGenerated]
  41:         get
  42:         {
  43:             return this.<LastName>k_BackingField;
  44:         }
  45:         [CompilerGenerated]
  46:         set
  47:         {
  48:             this.<LastName>k_BackingField = value;
  49:         }
  50:     }
  51:  
  52:     public UserProfile() { }           
  53: }
There are a few things to look out for when using this syntax. The compiler forces you to declare properties with both a get and a set. You also don't get any kind of "safety" features, such as ensuring that you don't allow a null value to be assigned to a string property. If you want these more "advanced" features, you'll still need to define your property the "old school" way. Posted on Wednesday, August 8, 2007 10:00 PM .NET (C#) | Back to top

Copyright © Scott Dorman | Powered by: GeeksWithBlogs.net