Skip to content

Commit 4ab4f83

Browse files
Add examples for ImmutableArray (#8865)
1 parent d3cc8b3 commit 4ab4f83

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Immutable;
3+
4+
static class ImmutableArraySnippets
5+
{
6+
static void ImmutableArrayNumbers()
7+
{
8+
// <SnippetIterate>
9+
// Create an immutable array of numbers
10+
ImmutableArray<int> numbers = ImmutableArray.Create(1, 2, 3, 4, -1, -2);
11+
12+
// Iterate over all items in the array and print them
13+
foreach (int n in numbers)
14+
{
15+
Console.Write(n);
16+
Console.Write(' ');
17+
}
18+
// Output: 1 2 3 4 -1 -2
19+
// </SnippetIterate>
20+
21+
// <SnippetModify>
22+
ImmutableArray<int> numbers2 = numbers.RemoveAt(0).Add(-3);
23+
// numbers2 will contain: 2 3 4 -1 -2 -3
24+
// </SnippetModify>
25+
26+
// <SnippetBuilder>
27+
// Create immutable array builder
28+
ImmutableArray<int>.Builder builder = ImmutableArray.CreateBuilder<int>();
29+
30+
// Iterate over all items in the original array and add positive elements to the builder
31+
for (int i = 0; i < numbers.Length; i++)
32+
{
33+
if (numbers[i] > 0) builder.Add(numbers[i]);
34+
}
35+
36+
// Create an immutable array from the contents of the builder
37+
ImmutableArray<int> numbers3 = builder.ToImmutable();
38+
// numbers3 will contain: 1 2 3 4
39+
// </SnippetBuilder>
40+
}
41+
42+
public static void Run()
43+
{
44+
ImmutableArrayNumbers();
45+
}
46+
}
47+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
class Program
4+
{
5+
static void Main()
6+
{
7+
ImmutableArraySnippets.Run();
8+
}
9+
}
10+

xml/System.Collections.Immutable/ImmutableArray`1.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@
107107
|**Operation**|<xref:System.Collections.Immutable.ImmutableArray%601> Complexity|<xref:System.Collections.Immutable.ImmutableList%601> Complexity|**Comments**|
108108
|**Item**|O(1)|O(log n)|Directly index into the underlying array|
109109
|**Add()**|O(n)|O(log n)|Requires creating a new array|
110+
111+
## Examples
112+
This example shows how to create an immutable array and iterate over elements in it:
113+
114+
:::code language="csharp" source="~/snippets/csharp/System.Collections.Immutable/ImmutableArray`1/Overview/ImmutableArraySnippets.cs" id="SnippetIterate":::
115+
116+
This example shows how to create a new immutable array by adding and removing items from the original array:
117+
118+
:::code language="csharp" source="~/snippets/csharp/System.Collections.Immutable/ImmutableArray`1/Overview/ImmutableArraySnippets.cs" id="SnippetModify":::
119+
120+
This example shows how to create an immutable array using <xref:System.Collections.Immutable.ImmutableArray%601.Builder>:
121+
122+
:::code language="csharp" source="~/snippets/csharp/System.Collections.Immutable/ImmutableArray`1/Overview/ImmutableArraySnippets.cs" id="SnippetBuilder":::
110123
111124
]]></format>
112125
</remarks>

0 commit comments

Comments
 (0)