Skip to content

Commit 7129aa9

Browse files
authored
Merge pull request #10398 from Meir017/patch-1
Update roslyn-analyzers-and-code-aware-library-for-immutablearrays.md
2 parents 970823a + 5062063 commit 7129aa9

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

docs/extensibility/roslyn-analyzers-and-code-aware-library-for-immutablearrays.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ Users are familiar with writing code like the following:
2929

3030
```csharp
3131
var a1 = new int[0];
32-
Console.WriteLine("a1.Length = { 0}", a1.Length);
32+
Console.WriteLine("a1.Length = {0}", a1.Length);
3333
var a2 = new int[] { 1, 2, 3, 4, 5 };
34-
Console.WriteLine("a2.Length = { 0}", a2.Length);
34+
Console.WriteLine("a2.Length = {0}", a2.Length);
3535
```
3636

3737
Creating empty arrays to fill in with subsequent lines of code and using collection initializer syntax are familiar to C# developers. However, writing the same code for an ImmutableArray crashes at run time:
3838

3939
```csharp
4040
var b1 = new ImmutableArray<int>();
41-
Console.WriteLine("b1.Length = { 0}", b1.Length);
41+
Console.WriteLine("b1.Length = {0}", b1.Length);
4242
var b2 = new ImmutableArray<int> { 1, 2, 3, 4, 5 };
43-
Console.WriteLine("b2.Length = { 0}", b2.Length);
43+
Console.WriteLine("b2.Length = {0}", b2.Length);
4444
```
4545

4646
The first error is due to ImmutableArray implementation's using a struct to wrap the underlying data storage. Structs must have parameter-less constructors so that `default(T)` expressions can return structs with all zero or null members. When the code accesses `b1.Length`, there is a run time null dereference error because there is no underlying storage array in the ImmutableArray struct. The correct way to create an empty ImmutableArray is `ImmutableArray<int>.Empty`.
@@ -61,7 +61,7 @@ The template opens a *DiagnosticAnalyzer.cs* file. Choose that editor buffer tab
6161

6262
```csharp
6363
[DiagnosticAnalyzer(LanguageNames.CSharp)]
64-
public class ImmutableArrayAnalyzerAnalyzer : DiagnosticAnalyzer
64+
public class ImmutableArrayAnalyzer : DiagnosticAnalyzer
6565
{}
6666
```
6767

@@ -222,7 +222,7 @@ namespace ImmutableArrayAnalyzer
222222
**Implement the property.** Fill in the `FixableDiagnosticIds` property's `get` body with the following code:
223223

224224
```csharp
225-
return ImmutableArray.Create(ImmutableArrayAnalyzerAnalyzer.DiagnosticId);
225+
return ImmutableArray.Create(ImmutableArrayAnalyzer.DiagnosticId);
226226
```
227227

228228
Roslyn brings together diagnostics and fixes by matching these identifiers, which are just strings. The project template generated a diagnostic ID for you, and you are free to change it. The code in the property just returns the ID from the analyzer class.

0 commit comments

Comments
 (0)