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 66cd354 commit 4073133
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docs/fundamentals/code-analysis/quality-rules/ca1833.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ When using a range-indexer on an array and implicitly assigning the value to <xr

## Rule description

Using a range-indexer on 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.Span%601> or <xref:System.Memory%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.Span%601> or <xref:System.Memory%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 +45,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 @@ -83,7 +83,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
Span<byte> tmp1 = arr[0..5];
Expand All @@ -96,7 +96,7 @@ class C
```csharp
class C
{
public void TestMethod(byte arr[])
public void TestMethod(byte[] arr)
{
// The violation fixed with explicit casting
Span<byte> tmp1 = (Span<byte>)arr[0..5];
Expand Down

0 comments on commit 4073133

Please sign in to comment.