C#idiomatic C#
ca1819fundamentals/code-analysis/quality-rules

Code analysis

CA1819: Properties should not return arrays

Learn about code analysis rule CA1819: Properties should not return arrays

CA1819 Performance enabled by default in .NET 10: No
Fix is breaking or non-breaking BreakingApplicable languages C# and Visual Basic

Cause

A property returns an array.

By default, this rule only looks at externally visible properties and types, but this is configurable.

Rule description

Arrays returned by properties are not write-protected, even if the property is read-only. To keep the array tamper-proof, the property must return a copy of the array. Typically, users won't understand the adverse performance implications of calling such a property. Specifically, they might use the property as an indexed property.

How to fix violations

To fix a violation of this rule, either make the property a method or change the property to return a collection.

When to suppress warnings

You can suppress a warning that's raised for a property of an attribute that's derived from the System.Attribute class. Attributes can contain properties that return arrays, but can't contain properties that return collections.

You can suppress the warning if the property is part of a Data Transfer Object (DTO) class.

Otherwise, do not suppress a warning from this rule.

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.

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

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

ini
[*.{cs,vb}]
dotnet_diagnostic.CA1819.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 (Performance) that it applies to. For more information, see Code quality rule configuration options.

Include specific API surfaces

You can configure which parts of your codebase to run this rule on, based on their accessibility, by setting the api_surface option. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an .editorconfig file in your project:

ini
dotnet_code_quality.CAXXXX.api_surface = private, internal

Example violation

The following example shows a property that violates this rule:

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1819.cssnippet1

csharp
using System.Collections.ObjectModel;

namespace ca1819
{
    //<snippet1>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = pages;
        }

        public string[] Pages { get; }
    }
    //</snippet1>
}

namespace ca1819_2
{
    //<snippet2>
    public class Book
    {
        private string[] _Pages;

        public Book(string[] pages)
        {
            _Pages = pages;
        }

        public string[] GetPages()
        {
            // Need to return a clone of the array so that consumers            
            // of this library cannot change its contents            
            return (string[])_Pages.Clone();
        }
    }
    //</snippet2>
}

namespace ca1819_3
{
    //<snippet3>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = new ReadOnlyCollection<string>(pages);
        }

        public ReadOnlyCollection<string> Pages { get; }
    }
    //</snippet3>
}

namespace ca1819_4
{
    //<snippet4>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = pages;
        }

        public string[] Pages { get; set; }
    }
    //</snippet4>
}

namespace ca1819_5
{
    //<snippet5>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = new Collection<string>(pages);
        }

        public Collection<string> Pages { get; }
    }
    //</snippet5>
}

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1819-properties-should-not-return-arrays_1.vbsnippet1

vb
Imports System
Imports System.Collections.ObjectModel

Namespace ca1819

    '<snippet1>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = pages
        End Sub

        Public ReadOnly Property Pages() As String()
    End Class
    '</snippet1>

End Namespace

Namespace ca1819_2

    '<snippet2>
    Public Class Book

        Private _Pages As String()

        Public Sub New(ByVal pages As String())
            _Pages = pages
        End Sub

        Public Function GetPages() As String()
            ' Need to return a clone of the array so that consumers            
            ' of this library cannot change its contents            
            Return DirectCast(_Pages.Clone(), String())
        End Function

    End Class
    '</snippet2>

End Namespace

Namespace ca1819_3

    '<snippet3>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = New ReadOnlyCollection(Of String)(pages)
        End Sub

        Public ReadOnly Property Pages() As ReadOnlyCollection(Of String)

    End Class
    '</snippet3>

End Namespace

Namespace ca1819_4

    '<snippet4>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = pages
        End Sub

        Public Property Pages() As String()

    End Class
    '</snippet4>

End Namespace

Namespace ca1819_5
    '<snippet5>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = New Collection(Of String)(pages)
        End Sub

        Public ReadOnly Property Pages() As Collection(Of String)
    End Class
    '</snippet5>

End Namespace

To fix a violation of this rule, either make the property a method or change the property to return a collection instead of an array.

Change the property to a method

The following example fixes the violation by changing the property to a method:

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1819-properties-should-not-return-arrays_1.vbsnippet2

vb
Imports System
Imports System.Collections.ObjectModel

Namespace ca1819

    '<snippet1>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = pages
        End Sub

        Public ReadOnly Property Pages() As String()
    End Class
    '</snippet1>

End Namespace

Namespace ca1819_2

    '<snippet2>
    Public Class Book

        Private _Pages As String()

        Public Sub New(ByVal pages As String())
            _Pages = pages
        End Sub

        Public Function GetPages() As String()
            ' Need to return a clone of the array so that consumers            
            ' of this library cannot change its contents            
            Return DirectCast(_Pages.Clone(), String())
        End Function

    End Class
    '</snippet2>

End Namespace

Namespace ca1819_3

    '<snippet3>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = New ReadOnlyCollection(Of String)(pages)
        End Sub

        Public ReadOnly Property Pages() As ReadOnlyCollection(Of String)

    End Class
    '</snippet3>

End Namespace

Namespace ca1819_4

    '<snippet4>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = pages
        End Sub

        Public Property Pages() As String()

    End Class
    '</snippet4>

End Namespace

Namespace ca1819_5
    '<snippet5>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = New Collection(Of String)(pages)
        End Sub

        Public ReadOnly Property Pages() As Collection(Of String)
    End Class
    '</snippet5>

End Namespace

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1819.cssnippet2

csharp
using System.Collections.ObjectModel;

namespace ca1819
{
    //<snippet1>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = pages;
        }

        public string[] Pages { get; }
    }
    //</snippet1>
}

namespace ca1819_2
{
    //<snippet2>
    public class Book
    {
        private string[] _Pages;

        public Book(string[] pages)
        {
            _Pages = pages;
        }

        public string[] GetPages()
        {
            // Need to return a clone of the array so that consumers            
            // of this library cannot change its contents            
            return (string[])_Pages.Clone();
        }
    }
    //</snippet2>
}

namespace ca1819_3
{
    //<snippet3>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = new ReadOnlyCollection<string>(pages);
        }

        public ReadOnlyCollection<string> Pages { get; }
    }
    //</snippet3>
}

namespace ca1819_4
{
    //<snippet4>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = pages;
        }

        public string[] Pages { get; set; }
    }
    //</snippet4>
}

namespace ca1819_5
{
    //<snippet5>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = new Collection<string>(pages);
        }

        public Collection<string> Pages { get; }
    }
    //</snippet5>
}

Change the property to return a collection

The following example fixes the violation by changing the property to return a System.Collections.ObjectModel.ReadOnlyCollection:

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1819.cssnippet3

csharp
using System.Collections.ObjectModel;

namespace ca1819
{
    //<snippet1>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = pages;
        }

        public string[] Pages { get; }
    }
    //</snippet1>
}

namespace ca1819_2
{
    //<snippet2>
    public class Book
    {
        private string[] _Pages;

        public Book(string[] pages)
        {
            _Pages = pages;
        }

        public string[] GetPages()
        {
            // Need to return a clone of the array so that consumers            
            // of this library cannot change its contents            
            return (string[])_Pages.Clone();
        }
    }
    //</snippet2>
}

namespace ca1819_3
{
    //<snippet3>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = new ReadOnlyCollection<string>(pages);
        }

        public ReadOnlyCollection<string> Pages { get; }
    }
    //</snippet3>
}

namespace ca1819_4
{
    //<snippet4>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = pages;
        }

        public string[] Pages { get; set; }
    }
    //</snippet4>
}

namespace ca1819_5
{
    //<snippet5>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = new Collection<string>(pages);
        }

        public Collection<string> Pages { get; }
    }
    //</snippet5>
}

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1819-properties-should-not-return-arrays_1.vbsnippet3

vb
Imports System
Imports System.Collections.ObjectModel

Namespace ca1819

    '<snippet1>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = pages
        End Sub

        Public ReadOnly Property Pages() As String()
    End Class
    '</snippet1>

End Namespace

Namespace ca1819_2

    '<snippet2>
    Public Class Book

        Private _Pages As String()

        Public Sub New(ByVal pages As String())
            _Pages = pages
        End Sub

        Public Function GetPages() As String()
            ' Need to return a clone of the array so that consumers            
            ' of this library cannot change its contents            
            Return DirectCast(_Pages.Clone(), String())
        End Function

    End Class
    '</snippet2>

End Namespace

Namespace ca1819_3

    '<snippet3>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = New ReadOnlyCollection(Of String)(pages)
        End Sub

        Public ReadOnly Property Pages() As ReadOnlyCollection(Of String)

    End Class
    '</snippet3>

End Namespace

Namespace ca1819_4

    '<snippet4>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = pages
        End Sub

        Public Property Pages() As String()

    End Class
    '</snippet4>

End Namespace

Namespace ca1819_5
    '<snippet5>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = New Collection(Of String)(pages)
        End Sub

        Public ReadOnly Property Pages() As Collection(Of String)
    End Class
    '</snippet5>

End Namespace

Allow users to modify a property

You might want to allow the consumer of the class to modify a property. The following example shows a read/write property that violates this rule:

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1819.cssnippet4

csharp
using System.Collections.ObjectModel;

namespace ca1819
{
    //<snippet1>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = pages;
        }

        public string[] Pages { get; }
    }
    //</snippet1>
}

namespace ca1819_2
{
    //<snippet2>
    public class Book
    {
        private string[] _Pages;

        public Book(string[] pages)
        {
            _Pages = pages;
        }

        public string[] GetPages()
        {
            // Need to return a clone of the array so that consumers            
            // of this library cannot change its contents            
            return (string[])_Pages.Clone();
        }
    }
    //</snippet2>
}

namespace ca1819_3
{
    //<snippet3>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = new ReadOnlyCollection<string>(pages);
        }

        public ReadOnlyCollection<string> Pages { get; }
    }
    //</snippet3>
}

namespace ca1819_4
{
    //<snippet4>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = pages;
        }

        public string[] Pages { get; set; }
    }
    //</snippet4>
}

namespace ca1819_5
{
    //<snippet5>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = new Collection<string>(pages);
        }

        public Collection<string> Pages { get; }
    }
    //</snippet5>
}

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1819-properties-should-not-return-arrays_1.vbsnippet4

vb
Imports System
Imports System.Collections.ObjectModel

Namespace ca1819

    '<snippet1>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = pages
        End Sub

        Public ReadOnly Property Pages() As String()
    End Class
    '</snippet1>

End Namespace

Namespace ca1819_2

    '<snippet2>
    Public Class Book

        Private _Pages As String()

        Public Sub New(ByVal pages As String())
            _Pages = pages
        End Sub

        Public Function GetPages() As String()
            ' Need to return a clone of the array so that consumers            
            ' of this library cannot change its contents            
            Return DirectCast(_Pages.Clone(), String())
        End Function

    End Class
    '</snippet2>

End Namespace

Namespace ca1819_3

    '<snippet3>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = New ReadOnlyCollection(Of String)(pages)
        End Sub

        Public ReadOnly Property Pages() As ReadOnlyCollection(Of String)

    End Class
    '</snippet3>

End Namespace

Namespace ca1819_4

    '<snippet4>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = pages
        End Sub

        Public Property Pages() As String()

    End Class
    '</snippet4>

End Namespace

Namespace ca1819_5
    '<snippet5>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = New Collection(Of String)(pages)
        End Sub

        Public ReadOnly Property Pages() As Collection(Of String)
    End Class
    '</snippet5>

End Namespace

The following example fixes the violation by changing the property to return a System.Collections.ObjectModel.Collection:

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1819-properties-should-not-return-arrays_1.vbsnippet5

vb
Imports System
Imports System.Collections.ObjectModel

Namespace ca1819

    '<snippet1>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = pages
        End Sub

        Public ReadOnly Property Pages() As String()
    End Class
    '</snippet1>

End Namespace

Namespace ca1819_2

    '<snippet2>
    Public Class Book

        Private _Pages As String()

        Public Sub New(ByVal pages As String())
            _Pages = pages
        End Sub

        Public Function GetPages() As String()
            ' Need to return a clone of the array so that consumers            
            ' of this library cannot change its contents            
            Return DirectCast(_Pages.Clone(), String())
        End Function

    End Class
    '</snippet2>

End Namespace

Namespace ca1819_3

    '<snippet3>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = New ReadOnlyCollection(Of String)(pages)
        End Sub

        Public ReadOnly Property Pages() As ReadOnlyCollection(Of String)

    End Class
    '</snippet3>

End Namespace

Namespace ca1819_4

    '<snippet4>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = pages
        End Sub

        Public Property Pages() As String()

    End Class
    '</snippet4>

End Namespace

Namespace ca1819_5
    '<snippet5>
    Public Class Book
        Public Sub New(ByVal pages As String())
            Me.Pages = New Collection(Of String)(pages)
        End Sub

        Public ReadOnly Property Pages() As Collection(Of String)
    End Class
    '</snippet5>

End Namespace

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1819.cssnippet5

csharp
using System.Collections.ObjectModel;

namespace ca1819
{
    //<snippet1>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = pages;
        }

        public string[] Pages { get; }
    }
    //</snippet1>
}

namespace ca1819_2
{
    //<snippet2>
    public class Book
    {
        private string[] _Pages;

        public Book(string[] pages)
        {
            _Pages = pages;
        }

        public string[] GetPages()
        {
            // Need to return a clone of the array so that consumers            
            // of this library cannot change its contents            
            return (string[])_Pages.Clone();
        }
    }
    //</snippet2>
}

namespace ca1819_3
{
    //<snippet3>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = new ReadOnlyCollection<string>(pages);
        }

        public ReadOnlyCollection<string> Pages { get; }
    }
    //</snippet3>
}

namespace ca1819_4
{
    //<snippet4>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = pages;
        }

        public string[] Pages { get; set; }
    }
    //</snippet4>
}

namespace ca1819_5
{
    //<snippet5>
    public class Book
    {
        public Book(string[] pages)
        {
            Pages = new Collection<string>(pages);
        }

        public Collection<string> Pages { get; }
    }
    //</snippet5>
}