Skip to content

Commit 4da0337

Browse files
authored
Use ReadOnlySpan where possible (#46344)
1 parent 576dfb9 commit 4da0337

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

docs/fundamentals/runtime-libraries/snippets/System/Span/Overview/csharp/Program2.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ class Program2
55
static void Run()
66
{
77
string contentLength = "Content-Length: 132";
8-
var length = GetContentLength(contentLength.ToCharArray());
8+
int length = GetContentLength(contentLength.ToCharArray());
99
Console.WriteLine($"Content length: {length}");
1010
}
1111

1212
private static int GetContentLength(ReadOnlySpan<char> span)
1313
{
14-
var slice = span.Slice(16);
14+
ReadOnlySpan<char> slice = span.Slice(16);
1515
return int.Parse(slice);
1616
}
1717
}
18+
1819
// Output:
1920
// Content length: 132

docs/fundamentals/runtime-libraries/snippets/System/Span/Overview/csharp/Project.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>Library</OutputType>
5-
<TargetFrameworks>net8</TargetFrameworks>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net9</TargetFrameworks>
66
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
77
</PropertyGroup>
88

docs/fundamentals/runtime-libraries/snippets/System/Span/Overview/csharp/program.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ private static void CreateSpanFromArray()
2020
{
2121
// <Snippet1>
2222
// Create a span over an array.
23-
var array = new byte[100];
24-
var arraySpan = new Span<byte>(array);
23+
byte[] array = new byte[100];
24+
Span<byte> arraySpan = new(array);
2525

2626
byte data = 0;
2727
for (int ctr = 0; ctr < arraySpan.Length; ctr++)
2828
arraySpan[ctr] = data++;
2929

3030
int arraySum = 0;
31-
foreach (var value in array)
31+
foreach (byte value in array)
3232
arraySum += value;
3333

3434
Console.WriteLine($"The sum is {arraySum}");
35+
3536
// Output: The sum is 4950
3637
// </Snippet1>
3738
}
@@ -40,7 +41,7 @@ private static void CreateSpanFromNativeMemory()
4041
{
4142
// <Snippet2>
4243
// Create a span from native memory.
43-
var native = Marshal.AllocHGlobal(100);
44+
nint native = Marshal.AllocHGlobal(100);
4445
Span<byte> nativeSpan;
4546
unsafe
4647
{
@@ -51,11 +52,12 @@ private static void CreateSpanFromNativeMemory()
5152
nativeSpan[ctr] = data++;
5253

5354
int nativeSum = 0;
54-
foreach (var value in nativeSpan)
55+
foreach (byte value in nativeSpan)
5556
nativeSum += value;
5657

5758
Console.WriteLine($"The sum is {nativeSum}");
5859
Marshal.FreeHGlobal(native);
60+
5961
// Output: The sum is 4950
6062
// </Snippet2>
6163
}
@@ -70,10 +72,11 @@ private static void CreateSpanFromStack()
7072
stackSpan[ctr] = data++;
7173

7274
int stackSum = 0;
73-
foreach (var value in stackSpan)
75+
foreach (byte value in stackSpan)
7476
stackSum += value;
7577

7678
Console.WriteLine($"The sum is {stackSum}");
79+
7780
// Output: The sum is 4950
7881
// </Snippet3>
7982
}
@@ -85,8 +88,8 @@ public class ProgramB
8588
public static void WorkWithSpans()
8689
{
8790
// Create a span over an array.
88-
var array = new byte[100];
89-
var arraySpan = new Span<byte>(array);
91+
byte[] array = new byte[100];
92+
Span<byte> arraySpan = new(array);
9093

9194
InitializeSpan(arraySpan);
9295
Console.WriteLine($"The sum is {ComputeSum(arraySpan):N0}");
@@ -118,14 +121,15 @@ public static void InitializeSpan(Span<byte> span)
118121
span[ctr] = value++;
119122
}
120123

121-
public static int ComputeSum(Span<byte> span)
124+
public static int ComputeSum(ReadOnlySpan<byte> span)
122125
{
123126
int sum = 0;
124-
foreach (var value in span)
127+
foreach (byte value in span)
125128
sum += value;
126129

127130
return sum;
128131
}
132+
129133
// The example displays the following output:
130134
// The sum is 4,950
131135
// The sum is 4,950

docs/fundamentals/runtime-libraries/system-span{t}.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: System.Span<T> struct
33
description: Learn about the System.Span\<T> struct.
4-
ms.date: 12/31/2023
4+
ms.date: 05/21/2025
55
dev_langs:
66
- CSharp
77
- FSharp
@@ -34,7 +34,7 @@ The following example uses the C# [stackalloc](/dotnet/csharp/language-reference
3434
:::code language="csharp" source="./snippets/System/Span/Overview/csharp/program.cs" id="Snippet3":::
3535
:::code language="fsharp" source="./snippets/System/Span/Overview/fsharp/program.fs" id="Snippet3":::
3636

37-
Because `Span<T>` is an abstraction over an arbitrary block of memory, methods of the `Span<T>` type and methods with `Span<T>` parameters operate on any `Span<T>` object regardless of the kind of memory it encapsulates. For example, each of the separate sections of code that initialize the span and calculate the sum of its elements can be changed into single initialization and calculation methods, as the following example illustrates:
37+
Because `Span<T>` is an abstraction over an arbitrary block of memory, methods of the `Span<T>` type and methods with `Span<T>` parameters operate on any `Span<T>` object regardless of the kind of memory it encapsulates. For example, each of the separate sections of code that initialize the span and calculate the sum of its elements can be refactored into single initialization and calculation methods, as the following example illustrates:
3838

3939
:::code language="csharp" source="./snippets/System/Span/Overview/csharp/program.cs" id="Snippet4":::
4040
:::code language="fsharp" source="./snippets/System/Span/Overview/fsharp/program.fs" id="Snippet4":::

0 commit comments

Comments
 (0)