|
1 | 1 | ---
|
2 | 2 | title: "CA2104: Do not declare read only mutable reference types"
|
3 |
| -ms.date: 11/04/2016 |
| 3 | +ms.date: 11/01/2018 |
4 | 4 | ms.prod: visual-studio-dev15
|
5 | 5 | ms.technology: vs-ide-code-analysis
|
6 | 6 | ms.topic: reference
|
@@ -30,25 +30,35 @@ ms.workload:
|
30 | 30 | |Category|Microsoft.Security|
|
31 | 31 | |Breaking Change|Non-breaking|
|
32 | 32 |
|
| 33 | +> [!NOTE] |
| 34 | +> Rule CA2104 is obsolete and will be removed in a future version of Visual Studio. |
| 35 | +
|
33 | 36 | ## 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. |
35 | 39 |
|
36 | 40 | ## 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. |
38 | 41 |
|
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. |
40 | 47 |
|
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. |
42 | 49 |
|
43 | 50 | ## 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. |
45 | 53 |
|
46 | 54 | ## 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. |
48 | 57 |
|
49 | 58 | ## Example
|
50 |
| - The following example shows a field declaration that causes a violation of this rule. |
51 | 59 |
|
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