You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/extensibility/roslyn-analyzers-and-code-aware-library-for-immutablearrays.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -29,18 +29,18 @@ Users are familiar with writing code like the following:
29
29
30
30
```csharp
31
31
vara1=newint[0];
32
-
Console.WriteLine("a1.Length = {0}", a1.Length);
32
+
Console.WriteLine("a1.Length = {0}", a1.Length);
33
33
vara2=newint[] { 1, 2, 3, 4, 5 };
34
-
Console.WriteLine("a2.Length = {0}", a2.Length);
34
+
Console.WriteLine("a2.Length = {0}", a2.Length);
35
35
```
36
36
37
37
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:
38
38
39
39
```csharp
40
40
varb1=newImmutableArray<int>();
41
-
Console.WriteLine("b1.Length = {0}", b1.Length);
41
+
Console.WriteLine("b1.Length = {0}", b1.Length);
42
42
varb2=newImmutableArray<int> { 1, 2, 3, 4, 5 };
43
-
Console.WriteLine("b2.Length = {0}", b2.Length);
43
+
Console.WriteLine("b2.Length = {0}", b2.Length);
44
44
```
45
45
46
46
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
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