Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 3.0
Tools
Development Tools
FxCop
FxCop Warnings
Design Warnings
 Do not declare visible instance fie...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio Team System
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. Accessing a property is as easy as accessing a field, and the code in a property's accessors can change as the type's features 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; there is no performance gain associated with using 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 using an externally visible property.

Do not exclude 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 Secured types should not expose fields.

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

C#
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 ; }
      }
   }
}

Protected fields are also considered visible.

Both C# (3.0 and higher) and C++/CLI support the ability to declare properties without the need to also declaring their field backing store. In C# these are called as Automatic Properties, whereas in C++/CLI they are known as Trivial Properties.

To declare an Automatic Property in C#, simply declare a property as normal, but then do not provide a body for the assessors.

In C++/CLI, to declare a Trivial Property, simply do not provide a body for the property.

Concepts

Link Demands

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Protected fields are also visible      David M. Kean   |   Edit   |   Show History

Protected fields are also considered visible:

[C#]
 
using System;
 
namespace DesignLibrary
{
    public class BadInstanceFields
    {
        // Violates rule DoNotDeclareVisibleInstanceFields.
        protected int instanceData = 32;
    }
 
    public class GoodInstanceFields
    {
        private int instanceData = 32;
 
        protected int InstanceData
        {
            get { return instanceData; }
            set { instanceData = value; }
        }
    }
}
Tags What's this?: Add a tag
Flag as ContentBug
Examples Break Rule: IdentifiersShouldDifferByMoreThanCase for protected/public mix      TheGreatToast   |   Edit   |   Show History
Note that both of the examples break the rule: IdentifiersShouldDifferByMoreThanCase if we were to have a public accessor on a proected field. A standard should be used to ensure protected instance fields differ in more than just case from their public accessors.
Tags What's this?: Add a tag
Flag as ContentBug
Declaring simple properties in C# and C++/CLI      David M. Kean   |   Edit   |   Show History

Both C# (3.0 and higher) and C++/CLI support the ability to declare properties without the need to also declaring their field backing store. In C# these are called as Automatic Properties, whereas, in C++/CLI they are known as Trivial Properties.

To declare an Automatic Property in C#, simply declare a property as normal, but then do not provide a body for the accessors.

[C#]
 
using System;
 
namespace Samples
{
public class Book
{
public string Title
{
get;
set;
}
}
}

In C++/CLI, to declare a Trivial Property, simply do not provide a body for the property.

[C++]
 
using namespace System;
 
namespace Samples
{
public ref class Book
{
public:
property String^ Title;
}
}
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker