Skip to content

Latest commit

 

History

History
138 lines (102 loc) · 5.26 KB

File metadata and controls

138 lines (102 loc) · 5.26 KB
title description ms.date f1_keywords helpviewer_keywords author ms.author dev_langs
CA1024: Use properties where appropriate (code analysis)
Learn about code analysis rule CA1024: Use properties where appropriate
12/21/2020
UsePropertiesWhereAppropriate
CA1024
CA1024
UsePropertiesWhereAppropriate
gewarren
gewarren
CSharp
VB

CA1024: Use properties where appropriate

Property Value
Rule ID CA1024
Title Use properties where appropriate
Category Design
Fix is breaking or non-breaking Breaking
Enabled by default in .NET 8 No

Cause

A method has a name that starts with Get, takes no parameters, and returns a value that's not an array.

By default, this rule only looks at externally visible methods, but this is configurable.

Rule description

In most cases, properties represent data and methods perform actions. Properties are accessed like fields, which makes them easier to use. A method is a good candidate to become a property if one of these conditions is present:

  • The method takes no arguments and returns the state information of an object.
  • The method accepts a single argument to set some part of the state of an object.

How to fix violations

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

When to suppress warnings

Suppress a warning from this rule if the method meets one of the following criteria. In these situations, a method is preferable to a property.

  • The method can't behave as a field.
  • The method performs a time-consuming operation. The method is perceivably slower than the time that is required to set or get the value of a field.
  • The method performs a conversion. Accessing a field does not return a converted version of the data that it stores.
  • The Get method has an observable side effect. Retrieving the value of a field does not produce any side effects.
  • The order of execution is important. Setting the value of a field does not rely on the occurrence of other operations.
  • Calling the method two times in succession creates different results.
  • The method is static but returns an object that can be changed by the caller. Retrieving the value of a field does not allow the caller to change the data that's stored by the field.
  • The method returns an array.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable CA1024
// The code that's violating the rule is on this line.
#pragma warning restore CA1024

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA1024.severity = none

For more information, see How to suppress code analysis warnings.

Configure code to analyze

Use the following option to configure which parts of your codebase to run this rule on.

You can configure this option for just this rule, for all rules it applies to, or for all rules in this category (Design) that it applies to. For more information, see Code quality rule configuration options.

[!INCLUDEapi-surface]

Example

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

:::code language="csharp" source="snippets/csharp/all-rules/ca1024.cs" id="snippet1":::

:::code language="vb" source="snippets/vb/all-rules/ca1024.vb" id="snippet1":::

Control property expansion in the debugger

One reason programmers avoid using a property is because they do not want the debugger to autoexpand it. For example, the property might involve allocating a large object or calling a P/Invoke, but it might not actually have any observable side effects.

You can prevent the debugger from auto-expanding properties by applying xref:System.Diagnostics.DebuggerBrowsableAttribute?displayProperty=fullName. The following example shows this attribute being applied to an instance property.

Imports System.Diagnostics

Namespace Microsoft.Samples
    Public Class TestClass
        ' [...]

        <DebuggerBrowsable(DebuggerBrowsableState.Never)> _
        Public ReadOnly Property LargeObject() As LargeObject
            Get
                ' Allocate large object
                ' [...]
            End Get
        End Property
    End Class
End Namespace
using System.Diagnostics;

namespace Microsoft.Samples
{
    class TestClass
    {
        // [...]

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        public LargeObject LargeObject
        {
            get
            {
                // Allocate large object
                // [...]
            }
        }
    }
}