Michael Gillson

Share ideas, observations, expertise  
Using C# and the .NET Frame Work.

Naming Conventions - Properties

The other naming convention that causes a lot of discussion is properties. (See Section 2.6 in Brad Abrams document) The question is what should the backing private field be named. I prefer a prefix of “_” on the variable. There seems to be three schools of thought on how to assign a public get property.  See also - Naming Conventions - Controls
 
1
   public class SampleClass1
   {
      private string myProperty;
      public string MyProperty
      {
         get { return myProperty; }
      }
 
      public SampleClass1(string mYProperty)
      {
         // Using this keyword is NOT always a protection against typos
         this.myProperty = myProperty;
      }
   }
 
     I purposely placed a typo error in the constructor. Visual Studio 2008 compiles but does give me a warning about assignment made to same variable.
 
 
2
   public class SampleClass2
   {
      private string m_MyProperty;
      public string MyProperty
      {
         get { return m_MyProperty; }
      }
 
      public SampleClass2(string myProperty)
      {
         m_MyProperty = myProperty;
      }
   }
 
     I was forced to remove the typo error in the constructor to get the code to compile. But IntelliSense will show all the m_ private fields. This can lead to clutter in Intellisense if I have other variables that start with the letter m. This style seems to be used because not all languages allow an “_” to begin a variable name.
 
3
   public class SampleClass3
   {
      private string _MyProperty;
      public string MyProperty
      {
         get { return _MyProperty; }
      }
 
      public SampleClass3(string myProperty)
      {
         _MyProperty = myProperty;
      }
   }
 
     This example is nearly identical to the previous example but now all the private fields will display in IntelliSense in an area that is out of the way. No more collisions with variable names.
     Still, this does not make the case to use “_” as the prefix. The practice I follow is to only assign to the private field directly in the property setter, nowhere else. 
     The following code example extends SampleClass3 to protect the property from NULL assignment. Notice the private scope in front of the word set. This property is still public get but it can only be set in this class.
 
   public class SampleClass3
   {
      private string _MyProperty = string.Empty;
      public string MyProperty
      {
         get { return _MyProperty; }
         private set
         {
            if (value == null)
               _MyProperty = string.Empty;
            else
               _MyProperty = value;
         }
      }
 
      public SampleClass3(string myProperty)
      {
         MyProperty = myProperty;
      }
   }
 
 
     Lastly, doing the assignment via the property setter anticipates C# 3.0 which allows for simple properties to have completely hidden private backing fields which means the field can only be set by property setter.
 
   public class SampleClass4
   {
      public string MyProperty { get; private set; }
 
      public SampleClass4(string myProperty)
      {
         MyProperty = myProperty;
      }
 
   }
 
       Another bonus of using the “_” prefix is prop and propg snippets can be altered to increase productivity. Now, I only type one name for both the backing field and the property name. I have also added more code to the propg snippet so Lazy Loading is easier to implement. The code samples below show IntelliSense when typing “pr” and prop and propg snippets in active mode.
 
 
 
 
 
     Snippets can be copied to “C:\Program Files\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#” or installed.  For some good information about snippets, view this blog.
 
 

Download snippet files - 2.2 Kb


Site Map Printable View powered by mojoPortal Content Management System Valid XHTML 1.0 Transitional Valid CSS Design by Pax Armonia