Hendelser
17. mars, 21 - 21. mars, 10
Bli med i meetup-serien for å bygge skalerbare AI-løsninger basert på virkelige brukstilfeller med andre utviklere og eksperter.
Registrer deg nåDenne nettleseren støttes ikke lenger.
Oppgrader til Microsoft Edge for å dra nytte av de nyeste funksjonene, sikkerhetsoppdateringene og den nyeste tekniske støtten.
Property | Value |
---|---|
Rule ID | CA1819 |
Title | Properties should not return arrays |
Category | Performance |
Fix is breaking or non-breaking | Breaking |
Enabled by default in .NET 9 | No |
A property returns an array.
By default, this rule only looks at externally visible properties and types, but this is configurable.
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.
To fix a violation of this rule, either make the property a method or change the property to return a collection.
You can suppress a warning that's raised for a property of an attribute that's derived from the 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.
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 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.
[*.{cs,vb}]
dotnet_diagnostic.CA1819.severity = none
For more information, see How to suppress code analysis warnings.
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.
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:
dotnet_code_quality.CAXXXX.api_surface = private, internal
Obs!
Replace the XXXX
part of CAXXXX
with the ID of the applicable rule.
The following example shows a property that violates this rule:
public class Book
{
private string[] _Pages;
public Book(string[] pages)
{
_Pages = pages;
}
public string[] Pages
{
get { return _Pages; }
}
}
Public Class Book
Public Sub New(ByVal pages As String())
Me.Pages = pages
End Sub
Public ReadOnly Property Pages() As String()
End Class
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.
The following example fixes the violation by changing the property to a method:
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
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();
}
}
The following example fixes the violation by changing the property to return a System.Collections.ObjectModel.ReadOnlyCollection<T>:
public class Book
{
private ReadOnlyCollection<string> _Pages;
public Book(string[] pages)
{
_Pages = new ReadOnlyCollection<string>(pages);
}
public ReadOnlyCollection<string> Pages
{
get { return _Pages; }
}
}
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
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:
public class Book
{
private string[] _Pages;
public Book(string[] pages)
{
_Pages = pages;
}
public string[] Pages
{
get { return _Pages; }
set { _Pages = value; }
}
}
Public Class Book
Public Sub New(ByVal pages As String())
Me.Pages = pages
End Sub
Public Property Pages() As String()
End Class
The following example fixes the violation by changing the property to return a System.Collections.ObjectModel.Collection<T>:
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
public class Book
{
private Collection<string> _Pages;
public Book(string[] pages)
{
_Pages = new Collection<string>(pages);
}
public Collection<string> Pages
{
get { return _Pages; }
}
}
.NET-tilbakemelding
.NET er et åpen kilde-prosjekt. Velg en kobling for å gi tilbakemelding:
Hendelser
17. mars, 21 - 21. mars, 10
Bli med i meetup-serien for å bygge skalerbare AI-løsninger basert på virkelige brukstilfeller med andre utviklere og eksperter.
Registrer deg nå