Skip to content

Commit

Permalink
fix code syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
gewarren committed Aug 10, 2023
1 parent 4fe4116 commit 66cd354
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions docs/fundamentals/code-analysis/quality-rules/ca1832.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ When using a range-indexer on an array and implicitly assigning the value to <xr

## Rule description

Using a range-indexer on an array and assigning to a memory or span type: The range indexer on a <xref:System.Span%601> is a non-copying <xref:System.Span%601.Slice%2A?#System_Span_1_Slice_System_Int32_System_Int32_> operation, but for the range indexer on array the method <xref:System.Runtime.CompilerServices.RuntimeHelpers.GetSubArray%2A> will be used instead of <xref:System.Span%601.Slice%2A?#System_Span_1_Slice_System_Int32_System_Int32_>, which produces a copy of requested portion of the array. This copy is usually unnecessary when it is implicitly used as a <xref:System.ReadOnlySpan%601> or <xref:System.ReadOnlyMemory%601> value. If a copy isn't intended, use the <xref:System.MemoryExtensions.AsSpan%2A?#System_MemoryExtensions_AsSpan__1___0___> or <xref:System.MemoryExtensions.AsMemory%2A?#System_MemoryExtensions_AsMemory__1___0___> method to avoid the unnecessary copy. If the copy is intended, either assign it to a local variable first or add an explicit cast. The analyzer only reports when an implicit cast is used on the result of the range indexer operation.
The range indexer on a <xref:System.Span%601> is a non-copying <xref:System.Span%601.Slice%2A?#System_Span_1_Slice_System_Int32_System_Int32_> operation. But for the range indexer on an array, the method <xref:System.Runtime.CompilerServices.RuntimeHelpers.GetSubArray%2A> will be used instead of <xref:System.Span%601.Slice%2A?#System_Span_1_Slice_System_Int32_System_Int32_>, which produces a copy of the requested portion of the array. This copy is usually unnecessary when it's implicitly used as a <xref:System.ReadOnlySpan%601> or <xref:System.ReadOnlyMemory%601> value. If a copy isn't intended, use the <xref:System.MemoryExtensions.AsSpan%2A?#System_MemoryExtensions_AsSpan__1___0___> or <xref:System.MemoryExtensions.AsMemory%2A?#System_MemoryExtensions_AsMemory__1___0___> method to avoid the unnecessary copy. If the copy is intended, either assign it to a local variable first or add an explicit cast.

The analyzer only reports when an implicit cast is used on the result of the range indexer operation.

### Detects

Expand All @@ -45,7 +47,7 @@ Explicit conversions:

## How to fix violations

To fix the violation of this rule: use the <xref:System.MemoryExtensions.AsSpan%2A?#System_MemoryExtensions_AsSpan__1___0___> or <xref:System.MemoryExtensions.AsMemory%2A?#System_MemoryExtensions_AsMemory__1___0___> extension method to avoid creating unnecessary data copies.
To fix a violation of this rule, use the <xref:System.MemoryExtensions.AsSpan%2A?#System_MemoryExtensions_AsSpan__1___0___> or <xref:System.MemoryExtensions.AsMemory%2A?#System_MemoryExtensions_AsMemory__1___0___> extension method to avoid creating unnecessary data copies.

```csharp
class C
Expand Down Expand Up @@ -74,7 +76,7 @@ class C
```

> [!TIP]
> A code fix is available for this rule in Visual Studio. To use it, position the cursor on the violation and press <kbd>Ctrl</kbd>+<kbd>.</kbd> (period). Choose **Use AsSpan instead of the Range-based indexer on an array** from the list of options that is presented.
> A code fix is available for this rule in Visual Studio. To use it, position the cursor on the violation and press <kbd>Ctrl</kbd>+<kbd>.</kbd> (period). Choose **Use AsSpan instead of the Range-based indexer on an array** from the list of options that's presented.
>
> ![Code fix for CA1832 - Use AsSpan or AsMemory instead of Range-based indexers for getting ReadOnlySpan or ReadOnlyMemory portion of an array](media/ca1832_codefix.png)
Expand All @@ -83,7 +85,7 @@ You can also avoid this warning by adding an explicit cast.
```csharp
class C
{
public void TestMethod(byte arr[])
public void TestMethod(byte[] arr)
{
// The violation occurs
ReadOnlySpan<byte> tmp1 = arr[0..2];
Expand All @@ -96,7 +98,7 @@ class C
```csharp
class C
{
public void TestMethod(byte arr[])
public void TestMethod(byte[] arr)
{
// The violation fixed with explicit casting
ReadOnlySpan<byte> tmp1 = (ReadOnlySpan<byte>)arr[0..2];
Expand Down

0 comments on commit 66cd354

Please sign in to comment.