This topic has not yet been rated Rate this topic

CA1051: Do not declare visible instance fields

TypeName

DoNotDeclareVisibleInstanceFields

CheckId

CA1051

Category

Microsoft.Design

Breaking Change

Breaking

An externally visible type has an externally visible instance field.

The primary use of a field should be as an implementation detail. Fields should be private or internal and should be exposed by using properties. It is as easy to access a property as it is to access a field, and the code in the accessors of a property can change as the features of the type expand without introducing breaking changes. Properties that just return the value of a private or internal field are optimized to perform on par with accessing a field; very little performance gain is associated with the use of externally visible fields over properties.

Externally visible refers to public, protected, and protected internal (Public, Protected, and Protected Friend in Visual Basic) accessibility levels.

To fix a violation of this rule, make the field private or internal and expose it by using an externally visible property.

Do not suppress a warning from this rule. Externally visible fields do not provide any benefits that are unavailable to properties. Additionally, public fields cannot be protected by Link Demands. See CA2112: Secured types should not expose fields.

The following example shows a type (BadPublicInstanceFields) that violates this rule. GoodPublicInstanceFields shows the corrected code.


using System;

namespace DesignLibrary
{
   public class BadPublicInstanceFields
   {
      // Violates rule DoNotDeclareVisibleInstanceFields.
      public int instanceData = 32;
   }

   public class GoodPublicInstanceFields
   {
      private int instanceData = 32;

      public int InstanceData
      {
         get { return instanceData; }
         set { instanceData = value ; }
      }
   }
}


Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Odd advice
This is really odd advice given that it's very common throughout the .net framework to declare a protected instance variable in a base class, and refer to it through the classes that inherit from said base class.
Properties are slower on some platforms
 Properties that just return the value of a private or internal field are optimized to perform on par with accessing a field; very little performance gain is associated with the use of externally visible fields over properties. $0$0 $0 $0This is not true on platforms using the compact framework - Xbox 360 and Windows Phone 7 - properties are never optimised/inlined and if called frequently as in game development scenarios can end up on your critical path.$0
Benefits of fields
Externally visible fields do not provide any benefits that are unavailable to properties.

Fields can be passed as ref or out parameters what may be considered as a benefit. However, the usage of ref and out parameters is not recommended for externally visible methods (CA1021, http://msdn.microsoft.com/en-us/library/ms182131.aspx ).