Use properties where appropriate

TypeName

UsePropertiesWhereAppropriate

CheckId

CA1024

Category

Microsoft.Design

Breaking Change

Breaking

Cause

A public or protected method has a name that begins with "Get," takes no parameters, and returns a value that is not an array.

Rule Description

In most cases, properties represent data, and methods perform actions. Properties are accessed like fields, which makes them easier to use. If a method takes no arguments and returns an object's state information, or accepts a single argument to set some part of an object's state, it is a good candidate for becoming a property.

Properties should behave as if they are fields; if the method cannot, it should not be changed to a property. Methods are preferable to properties in the following situations:

  • The method performs a time-consuming operation. The method is perceivably slower than the time it takes to set or get a field's value.

  • The method performs a conversion. Accessing a field does not return a converted version of the data it stores.

  • The "Get" method has an observable side effect. Retrieving a field's value does not produce any side effects.

  • The order of execution is important. Setting the value of a field does not rely on other operations having occurred.

  • Calling the method twice in succession creates different results.

  • The method is static but returns an object that can be changed by the caller. Retrieving a field's value does not allow the caller to change the data stored by the field.

  • The method returns an array.

How to Fix Violations

To fix a violation of this rule, change the method to a property.

When to Exclude Warnings

Exclude a warning from this rule if the method meets at least one of the previously listed criteria.

Example

The following example contains several methods that should be converted to properties, and several that should not because they do not behave like fields.

using System;
using System.Globalization;
using System.Collections;
namespace DesignLibrary
{
   // Illustrates the behavior of rule: 
   //  UsePropertiesWhereAppropriate.

   public class Appointment
   {
      static long nextAppointmentID;
      static double[] discountScale = {5.0, 10.0, 33.0};
      string customerName;
      long customerID;
      DateTime when;

      // Static constructor.
      static Appointment()
      {
         // Initializes the static variable for Next appointment ID.
      }

      // This method will violate the rule, but should not be a property.
      // This method has an observable side effect. 
      // Calling the method twice in succession creates different results.
      public static long GetNextAvailableID()
      {
         nextAppointmentID++;
         return nextAppointmentID - 1;
      }

      // This method will violate the rule, but should not be a property.
      // This method performs a time-consuming operation. 
      // This method returns an array.

      public Appointment[] GetCustomerHistory()
      {
         // Connect to a database to get the customer's appointment history.
         return LoadHistoryFromDB(customerID);
      }

      // This method will violate the rule, but should not be a property.
      // This method is static but returns a mutable object.
      public static double[] GetDiscountScaleForUpdate()
      {
         return discountScale;
      }

      // This method will violate the rule, but should not be a property.
      // This method performs a conversion.
      public string GetWeekDayString()
      {
         return DateTimeFormatInfo.CurrentInfo.GetDayName(when.DayOfWeek);
      }
         
      // These methods will violate the rule, and should be properties.
      // They each set or return a piece of the current object's state.

      public DayOfWeek GetWeekDay ()
      {
         return when.DayOfWeek;
      }

      public void  SetCustomerName (string customerName)
      {
         this.customerName = customerName;
      }
      public string GetCustomerName ()
      {
         return customerName;
      }

     public void SetCustomerID (long customerID)
      {
         this.customerID = customerID;
      }

      public long GetCustomerID ()
      {
         return customerID;
      }
      
      public void SetScheduleTime (DateTime when)
      {
         this.when = when;
      }

      public DateTime GetScheduleTime ()
      {
         return when;
      }

      // Time-consuming method that is called by GetCustomerHistory.
      Appointment[] LoadHistoryFromDB(long customerID)
      {
         ArrayList records = new ArrayList();
         // Load from database.
         return (Appointment[])records.ToArray();
      }
   }
}