The following rules outline the usage guidelines for fields:
  • Do not use instance fields that are public or protected. If you avoid exposing fields directly to the developer, classes can be versioned more easily because a field cannot be changed to a property while maintaining binary compatibility. Consider providing get and set property accessors for fields instead of making them public. The presence of executable code in get and set property accessors allows later improvements, such as creation of an object on demand, upon usage of the property, or upon a property change notification. The following code example illustrates the correct use of private instance fields with get and set property accessors.
public struct Point {
   private int xValue;
   private int yValue;
   
   public Point(int x, int y) {
      this.xValue = x;
      this.yValue = y;
   }

   public int X {
      get {
         return xValue; 
      }
      set { 
         xValue = value; 
      }
   }
   public int Y {
      get { 
         return yValue; 
      }
      set { 
         yValue = value; 
      }
   }
}
  • Avoid creating fields for properties altogether when possible. If you can use the "short syntax" for properties, then that is the preferred syntax.
  • Expose a field to a derived class by using a protected property that returns the value of the field. This is illustrated in the following code example.
public class Control: Component {
   private int handle;
   protected int Handle {
      get { 
         return handle; 
      }
   }
}
  • Use the const keyword to declare constant fields that will not change. Language compilers save the values of const fields directly in calling code.
    • Use UPPERCASE for const field names
  • Use public static read-only fields for predefined object instances. If there are predefined instances of an object, declare them as public static read-only fields of the object itself. Use UPPERCASE for the field name. The following code example illustrates the correct use of public static read-only fields.
public struct Color {
   public static readonly Color RED = new Color(0x0000FF);
   public static readonly Color GREEN = new Color(0x00FF00);
   public static readonly Color BLUE = new Color(0xFF0000);
   public static readonly Color BLACK = new Color(0x000000);
   public static readonly Color WHITE = new Color(0xFFFFFF);
 }
  • Spell out all words used in a field name. Use abbreviations only if developers generally understand them. Do not use uppercase letters for field names unless they are const or static read-only. The following is an example of correctly named fields.

class SampleClass {
  string url;
  string destinationUrl;
}
  • Do not use Hungarian notation for field names. Good names describe semantics, not type.
  • Do not apply a prefix to field names or static field names. Specifically, do not apply a prefix to a field name to distinguish between static and nonstatic fields. For example, applying a g or s prefix is incorrect.
    • the only allowed prefix is to start field names with an underscore (_).

Last edited Jul 19, 2010 at 12:29 PM by camalot, version 1