Udostępnij za pośrednictwem


Field Usage Guidelines

The following rules outline the usage guidelines for fields:

  • Do not use instance fields that are public or protected (Public or Protected in Visual Basic). 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 Structure Point
       Private xValue As Integer
       Private yValue As Integer
    
       Public Sub New(x As Integer, y As Integer)
          Me.xValue = x
          Me.yValue = y
       End Sub 
    
       Public Property X() As Integer
          Get
             Return xValue
          End Get
          Set
             xValue = value
          End Set
       End Property
       Public Property Y() As Integer
          Get
             Return yValue
          End Get
          Set
             yValue = value
          End Set
       End Property
    End Structure 
    [C#]
    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; 
          }
       }
    }
    
  • 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
          Inherits Component
          Private handle As Integer
    
          Protected ReadOnly Property Handle() As Integer
             Get
                Return handle
             End Get
          End Property
       End Class 
    [C#]
    public class Control: Component
    {
       private int handle;
       protected int Handle
       {
          get
          { 
             return handle; 
          }
       }
    }
    
  • Use the const (Const in Visual Basic) keyword to declare constant fields that will not change. Language compilers save the values of const fields directly in calling code.

  • 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 Pascal case because the fields are public. The following code example illustrates the correct use of public static read-only fields.

    Public Structure Color
       Public Shared Red As New Color(&HFF)
       Public Shared Green As New Color(&HFF00)
       Public Shared Blue As New Color(&HFF0000)
       Public Shared Black As New Color(&H0)
       Public Shared White As New Color(&HFFFFFF)   
    
       Public Sub New(rgb As Integer)
       ' Insert code here.
       End Sub 
    
       Public Sub New(r As Byte, g As Byte, b As Byte)
          ' Insert code here.
       End Sub    
    
       Public ReadOnly Property RedValue() As Byte
          Get
             Return Color
          End Get
       End Property   
    
       Public ReadOnly Property GreenValue() As Byte
          Get
             Return Color
          End Get
       End Property
    
       Public ReadOnly Property BlueValue() As Byte
          Get
             Return Color
          End Get
       End Property
    End Structure 
    [C#]
    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);
    
       public Color(int rgb)
       { // Insert code here.}
       public Color(byte r, byte g, byte b)
       { // Insert code here.}
    
       public byte RedValue 
       { 
          get
          {
             return Color;
          }
       }
       public byte GreenValue 
       { 
          get
          {
             return Color;
          }
       }
       public byte BlueValue
       { 
          get
          {
             return Color;
          }
       }
    }
    
  • 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. The following is an example of correctly named fields.

    Class SampleClass
       Private url As String
       Private destinationUrl As String
    End Class
    [C#]
    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.

See Also

Design Guidelines for Class Library Developers | Class Member Usage Guidelines | Static Field Naming Guidelines