Skip to content

Commit 921c347

Browse files
authored
Merge pull request #3207 from gewarren/code-an
Update ca2104 about immutable types
2 parents e467b52 + eba2f4d commit 921c347

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

docs/code-quality/ca2104-do-not-declare-read-only-mutable-reference-types.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "CA2104: Do not declare read only mutable reference types"
3-
ms.date: 11/04/2016
3+
ms.date: 11/01/2018
44
ms.prod: visual-studio-dev15
55
ms.technology: vs-ide-code-analysis
66
ms.topic: reference
@@ -30,25 +30,35 @@ ms.workload:
3030
|Category|Microsoft.Security|
3131
|Breaking Change|Non-breaking|
3232

33+
> [!NOTE]
34+
> Rule CA2104 is obsolete and will be removed in a future version of Visual Studio.
35+
3336
## Cause
34-
An externally visible type contains an externally visible read-only field that is a mutable reference type.
37+
38+
An externally visible type contains an externally visible read-only field that is a mutable reference type.
3539

3640
## Rule description
37-
A mutable type is a type whose instance data can be modified. The <xref:System.Text.StringBuilder?displayProperty=fullName> class is an example of a mutable reference type. It contains members that can change the value of an instance of the class. An example of an immutable reference type is the <xref:System.String?displayProperty=fullName> class. After it has been instantiated, its value can never change.
3841

39-
The read-only modifier ([readonly](/dotnet/csharp/language-reference/keywords/readonly) in C#, [ReadOnly](/dotnet/visual-basic/language-reference/modifiers/readonly) in [!INCLUDE[vbprvb](../code-quality/includes/vbprvb_md.md)], and [const](/cpp/cpp/const-cpp) in C++) on a reference type field (pointer in C++) prevents the field from being replaced by a different instance of the reference type. However, the modifier does not prevent the instance data of the field from being modified through the reference type.
42+
A mutable type is a type whose instance data can be modified. The <xref:System.Text.StringBuilder?displayProperty=fullName> class is an example of a mutable reference type. It contains members that can change the value of an instance of the class. An example of an immutable reference type is the <xref:System.String?displayProperty=fullName> class. After it has been instantiated, its value can never change.
43+
44+
The read-only modifier ([readonly](/dotnet/csharp/language-reference/keywords/readonly) in C#, [ReadOnly](/dotnet/visual-basic/language-reference/modifiers/readonly) in Visual Basic, and [const](/cpp/cpp/const-cpp) in C++) on a reference type field (or pointer in C++) prevents the field from being replaced by a different instance of the reference type. However, the modifier does not prevent the instance data of the field from being modified through the reference type.
45+
46+
This rule may inadvertently show a violation for a type that is, in fact, immutable. In that case, it's safe to suppress the warning.
4047

41-
Read-only array fields are exempt from this rule but instead cause a violation of the [CA2105: Array fields should not be read only](../code-quality/ca2105-array-fields-should-not-be-read-only.md) rule.
48+
Read-only array fields are exempt from this rule but instead cause a violation of the [CA2105: Array fields should not be read only](../code-quality/ca2105-array-fields-should-not-be-read-only.md) rule.
4249

4350
## How to fix violations
44-
To fix a violation of this rule, remove the read-only modifier or, if a breaking change is acceptable, replace the field with an immutable type.
51+
52+
To fix a violation of this rule, remove the read-only modifier or, if a breaking change is acceptable, replace the field with an immutable type.
4553

4654
## When to suppress warnings
47-
It is safe to suppress a warning from this rule if the field type is immutable.
55+
56+
It's safe to suppress a warning from this rule if the field type is immutable.
4857

4958
## Example
50-
The following example shows a field declaration that causes a violation of this rule.
5159

52-
[!code-cpp[FxCop.Security.MutableReferenceTypes#1](../code-quality/codesnippet/CPP/ca2104-do-not-declare-read-only-mutable-reference-types_1.cpp)]
53-
[!code-csharp[FxCop.Security.MutableReferenceTypes#1](../code-quality/codesnippet/CSharp/ca2104-do-not-declare-read-only-mutable-reference-types_1.cs)]
54-
[!code-vb[FxCop.Security.MutableReferenceTypes#1](../code-quality/codesnippet/VisualBasic/ca2104-do-not-declare-read-only-mutable-reference-types_1.vb)]
60+
The following example shows a field declaration that causes a violation of this rule:
61+
62+
[!code-cpp[FxCop.Security.MutableReferenceTypes#1](../code-quality/codesnippet/CPP/ca2104-do-not-declare-read-only-mutable-reference-types_1.cpp)]
63+
[!code-csharp[FxCop.Security.MutableReferenceTypes#1](../code-quality/codesnippet/CSharp/ca2104-do-not-declare-read-only-mutable-reference-types_1.cs)]
64+
[!code-vb[FxCop.Security.MutableReferenceTypes#1](../code-quality/codesnippet/VisualBasic/ca2104-do-not-declare-read-only-mutable-reference-types_1.vb)]

0 commit comments

Comments
 (0)