Skip to content

Commit 5ce6253

Browse files
authored
Modernize code examples for CA rules (#48745)
1 parent fd4d88a commit 5ce6253

36 files changed

+169
-282
lines changed

docs/fundamentals/code-analysis/quality-rules/ca1045.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ Passing types by reference (using `out` or `ref`) requires experience with point
3131

3232
When a reference type is passed "by reference," the method intends to use the parameter to return a different instance of the object. (Passing a reference type by reference is also known as using a double pointer, pointer to a pointer, or double indirection.) Using the default calling convention, which is pass "by value," a parameter that takes a reference type already receives a pointer to the object. The pointer, not the object to which it points, is passed by value. Passing by value means that the method cannot change the pointer to have it point to a new instance of the reference type, but can change the contents of the object to which it points. For most applications this is sufficient and yields the behavior that you want.
3333

34-
If a method must return a different instance, use the return value of the method to accomplish this. See the <xref:System.String?displayProperty=fullName> class for a variety of methods that operate on strings and return a new instance of a string. By using this model, it is left to the caller to decide whether the original object is preserved.
34+
If a method must return a different instance, use the return value of the method to accomplish this. For methods that operate on strings and return a new instance of a string, see the <xref:System.String?displayProperty=fullName> class. By using this model, it is left to the caller to decide whether the original object is preserved.
3535

3636
Although return values are commonplace and heavily used, the correct application of `out` and `ref` parameters requires intermediate design and coding skills. Library architects who design for a general audience should not expect users to become proficient in working with `out` or `ref` parameters.
3737

3838
> [!NOTE]
39-
> When you work with parameters that are large structures, the additional resources that are required to copy these structures could cause a performance effect when you pass by value. In these cases, you might consider using `ref` or `out` parameters.
39+
> When you work with parameters that are large structures, the additional resources that are required to copy these structures could have a performance effect when you pass by value. In these cases, you might consider using `ref` or `out` parameters.
4040
4141
## How to fix violations
4242

43-
To fix a violation of this rule that is caused by a value type, have the method return the object as its return value. If the method must return multiple values, redesign it to return a single instance of an object that holds the values.
43+
To fix a violation of this rule that's caused by a value type, have the method return the object as its return value. If the method must return multiple values, redesign it to return a single instance of an object that holds the values.
4444

45-
To fix a violation of this rule that is caused by a reference type, make sure that the behavior that you want is to return a new instance of the reference. If it is, the method should use its return value to do this.
45+
To fix a violation of this rule that's caused by a reference type, make sure that the behavior that you want is to return a new instance of the reference. If it is, the method should use its return value to do this.
4646

4747
## When to suppress warnings
4848

docs/fundamentals/code-analysis/quality-rules/ca1806.md

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ There are several possible reasons for this warning:
3030

3131
- A new object is created but never used.
3232
- A method that creates and returns a new string is called and the new string is never used.
33-
- A COM or P/Invoke method that returns a `HRESULT` or error code that's never used.
34-
- A language-integrated query (LINQ) method that returns a result that's never used.
33+
- A COM or P/Invoke method returns a `HRESULT` or error code that's never used.
34+
- A language-integrated query (LINQ) method returns a result that's never used.
3535

3636
## Rule description
3737

@@ -110,33 +110,22 @@ dotnet_code_quality.CA1806.additional_use_results_methods = M:MyNamespace.MyType
110110
The following example shows a class that ignores the result of calling <xref:System.String.Trim%2A?displayProperty=nameWithType>.
111111

112112
:::code language="csharp" source="snippets/csharp/all-rules/ca1806.cs" id="snippet1":::
113-
114113
:::code language="vb" source="snippets/vb/all-rules/ca1806-do-not-ignore-method-results_1.vb" id="snippet1":::
115114

116-
## Example 2
117-
118-
The following example fixes the [Example 1](#example-1) violation by assigning the result of <xref:System.String.Trim%2A?displayProperty=nameWithType> back to the variable it was called on.
115+
The following example fixes the violation by assigning the result of <xref:System.String.Trim%2A?displayProperty=nameWithType> back to the variable it was called on.
119116

120117
:::code language="csharp" source="snippets/csharp/all-rules/ca1806.cs" id="snippet2":::
121-
122118
:::code language="vb" source="snippets/vb/all-rules/ca1806-do-not-ignore-method-results_1.vb" id="snippet2":::
123119

124-
## Example 3
120+
## Example 2
125121

126-
The following example shows a method that does not use an object that it creates.
122+
The following example shows a method that doesn't use an object that it creates.
127123

128124
> [!NOTE]
129125
> This violation cannot be reproduced in Visual Basic.
130126
131127
:::code language="csharp" source="snippets/csharp/all-rules/ca1806.cs" id="snippet3":::
132128

133-
## Example 4
134-
135-
The following example fixes the [Example 3](#example-3) violation by removing the unnecessary creation of an object.
129+
The following example fixes the violation by removing the unnecessary creation of an object.
136130

137131
:::code language="csharp" source="snippets/csharp/all-rules/ca1806.cs" id="snippet4":::
138-
139-
<!-- Examples don't exist for the following...
140-
The following example shows a method that ignores the error code that the native method GetShortPathName returns.
141-
The following example fixes the previous violation by checking the error code and throwing an exception when the call fails.
142-
-->

docs/fundamentals/code-analysis/quality-rules/ca1816.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Violations of this rule can be caused by:
3636

3737
## Rule description
3838

39-
The <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method lets users release resources at any time before the object becoming available for garbage collection. If the <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method is called, it frees resources of the object. This makes finalization unnecessary. <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> should call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> so the garbage collector doesn't call the finalizer of the object.
39+
The <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method lets users release resources at any time before the object becomes available for garbage collection. If the <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method is called, it frees resources of the object. This makes finalization unnecessary. <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> should call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> so the garbage collector doesn't call the finalizer of the object.
4040

4141
To prevent derived types with finalizers from having to reimplement <xref:System.IDisposable> and to call it, unsealed types without finalizers should still call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>.
4242

docs/fundamentals/code-analysis/quality-rules/ca2100.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "CA2100: Review SQL queries for security vulnerabilities (code analysis)"
33
description: "Learn about code analysis rule CA2100: Review SQL queries for security vulnerabilities"
4-
ms.date: 11/04/2016
4+
ms.date: 09/24/2025
55
f1_keywords:
66
- Review SQL queries for security vulnerabilities
77
- ReviewSqlQueriesForSecurityVulnerabilities
@@ -37,7 +37,7 @@ This rule assumes that any string whose value can't be determined at compile tim
3737
- Use a parameterized command string.
3838
- Validate the user input for both type and content before you build the command string.
3939

40-
The following .NET types implement the <xref:System.Data.IDbCommand.CommandText%2A> property or provide constructors that set the property by using a string argument.
40+
The following .NET types implement the <xref:System.Data.IDbCommand.CommandText%2A> property or provide constructors that set the property by using a string argument:
4141

4242
- <xref:System.Data.Odbc.OdbcCommand?displayProperty=fullName> and <xref:System.Data.Odbc.OdbcDataAdapter?displayProperty=fullName>
4343
- <xref:System.Data.OleDb.OleDbCommand?displayProperty=fullName> and <xref:System.Data.OleDb.OleDbDataAdapter?displayProperty=fullName>
@@ -64,7 +64,7 @@ To fix a violation of this rule, use a parameterized query.
6464

6565
## When to suppress warnings
6666

67-
It is safe to suppress a warning from this rule if the command text does not contain any user input.
67+
It's safe to suppress a warning from this rule if the command text does not contain any user input.
6868

6969
## Suppress a warning
7070

docs/fundamentals/code-analysis/quality-rules/ca2235.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ For more information, see [How to suppress code analysis warnings](../suppress-w
6767
The following example shows two types: one that violates the rule and one that satisfies the rule.
6868

6969
:::code language="csharp" source="snippets/csharp/all-rules/ca2235.cs" id="snippet1":::
70-
7170
:::code language="vb" source="snippets/vb/all-rules/ca2235-mark-all-non-serializable-fields_1.vb":::
7271

7372
## Remarks

docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ dotnet_diagnostic.CA1822.severity = none
1717

1818
# CA2200: Rethrow to preserve stack details
1919
dotnet_diagnostic.CA2200.severity = suggestion
20+
21+
# CA2100
22+
dotnet_diagnostic.CA2100.severity = warning

docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1002.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22

33
namespace ca1001
44
{
@@ -8,7 +8,7 @@ public class MutableItems
88
{
99
// CA1002: Change 'List<string>' in 'MutableItems.Items' to
1010
// use 'Collection<T>', 'ReadOnlyCollection<T>' or 'KeyedCollection<K,V>'.
11-
public List<string> Items { get; } = new List<string>();
11+
public List<string> Items { get; } = [];
1212

1313
public void Add(string item)
1414
{
@@ -19,7 +19,7 @@ public void Add(string item)
1919
// This class satisfies the rule.
2020
public class ReadOnlyItems
2121
{
22-
private readonly List<string> _items = new List<string>();
22+
private readonly List<string> _items = [];
2323

2424
public IReadOnlyCollection<string> Items => _items.AsReadOnly();
2525

docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1024.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public DateTime GetScheduleTime()
9292
// Time-consuming method that is called by GetCustomerHistory.
9393
Appointment[] LoadHistoryFromDB(long customerID)
9494
{
95-
ArrayList records = new ArrayList();
95+
ArrayList records = [];
9696
// Load from database.
9797
return (Appointment[])records.ToArray();
9898
}

docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1031.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.IO;
33

44
namespace ca1031
@@ -7,14 +7,14 @@ namespace ca1031
77
// Creates two violations of the rule.
88
public class GenericExceptionsCaught
99
{
10-
FileStream? inStream;
11-
FileStream? outStream;
10+
private readonly FileStream? _inStream;
11+
private readonly FileStream? _outStream;
1212

1313
public GenericExceptionsCaught(string inFile, string outFile)
1414
{
1515
try
1616
{
17-
inStream = File.Open(inFile, FileMode.Open);
17+
_inStream = File.Open(inFile, FileMode.Open);
1818
}
1919
catch (SystemException)
2020
{
@@ -23,7 +23,7 @@ public GenericExceptionsCaught(string inFile, string outFile)
2323

2424
try
2525
{
26-
outStream = File.Open(outFile, FileMode.Open);
26+
_outStream = File.Open(outFile, FileMode.Open);
2727
}
2828
catch
2929
{
@@ -34,28 +34,28 @@ public GenericExceptionsCaught(string inFile, string outFile)
3434

3535
public class GenericExceptionsCaughtFixed
3636
{
37-
FileStream? inStream;
38-
FileStream outStream;
37+
private readonly FileStream? _inStream;
38+
private readonly FileStream _outStream;
3939

4040
public GenericExceptionsCaughtFixed(string inFile, string outFile)
4141
{
4242
try
4343
{
44-
inStream = File.Open(inFile, FileMode.Open);
44+
_inStream = File.Open(inFile, FileMode.Open);
4545
}
4646

4747
// Fix the first violation by catching a specific exception.
4848
catch (FileNotFoundException)
4949
{
5050
Console.WriteLine($"Unable to open {inFile}.");
51-
};
51+
}
5252

5353
// For functionally equivalent code, also catch
5454
// remaining exceptions that may be thrown by File.Open
5555

5656
try
5757
{
58-
outStream = File.Open(outFile, FileMode.Open);
58+
_outStream = File.Open(outFile, FileMode.Open);
5959
}
6060

6161
// Fix the second violation by rethrowing the generic

docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1032.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Runtime.Serialization;
32

43
namespace ca1032
54
{

0 commit comments

Comments
 (0)