# SonarAnalyzer.Rules.CSharp.UnusedPrivateMember exception

**URL:** <https://community.sonarsource.com/t/sonaranalyzer-rules-csharp-unusedprivatemember-exception/104771>\
**Category:** Visual Studio\
**Tags:** dotnet\
**Created:** [November 21, 2023, 3:53pm UTC](https://community.sonarsource.com/t/sonaranalyzer-rules-csharp-unusedprivatemember-exception/104771 "2023-11-21T15:53:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![milbrandt](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/milbrandt/32/3726_2.png) [@milbrandt](https://community.sonarsource.com/u/milbrandt)\
**Post date:** [November 21, 2023, 3:53pm UTC](https://community.sonarsource.com/t/sonaranalyzer-rules-csharp-unusedprivatemember-exception/104771/1 "2023-11-21T15:53:04Z")

</div>

- Operating system: Windows 10
- Visual Studio version: 17.8
- SonarLint plugin version: 7.3.0.77872
- Programming language you’re coding in: C# 12 (.NET SDK 8.0.100)
- Is connected mode used: no

In a project with only one source file yet the analyzer reports

`|Warning|AD0001|Analyzer 'SonarAnalyzer.Rules.CSharp.UnusedPrivateMember' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.`

Tried to clean everything (`git clean -fdx`) and rebuild the solution. The warning remains.

As only one short source file is affected, I include the code here:

```csharp
using System.Numerics;
using SkiaSharp;

namespace Test.Geometry;

public class Geometry2D : IDisposable
{
    private readonly SKPath geometry = new();
    private readonly SKPathMeasure geometryEval = new();
    private bool disposedValue;

    public Geometry2D(Vector2 start, Vector2 end)
    {
        SKPoint[] points = [new(start.X, start.Y), new(end.X, end.Y)];
        geometry.AddPoly(points, close: false);
        geometryEval.SetPath(geometry);
    }

    public double Length => geometryEval.Length;

    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                // dispose managed state (managed objects)
                geometryEval.Dispose();
                geometry.Dispose();
            }

            // free unmanaged resources (unmanaged objects) and override finalizer
            // set large fields to null
            disposedValue = true;
        }
    }

    public void Dispose()
    {
        // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
        Dispose(disposing: true);
        GC.SuppressFinalize(this);
    }
}

```

---

<div class="post-metadata">

**Author:** ![PTR](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ptr/32/51783_2.png) [@PTR](https://community.sonarsource.com/u/PTR)\
**Post date:** [November 22, 2023, 2:20pm UTC](https://community.sonarsource.com/t/sonaranalyzer-rules-csharp-unusedprivatemember-exception/104771/2 "2023-11-22T14:20:05Z")

</div>

Same here, since VS2022 updated to v17.8. Sonarlint v7.4.0.80741

Avertissement AD0001 L’analyseur ‘SonarAnalyzer.Rules.CSharp.UnusedPrivateMember’ a levé une exception de type ‘System.NullReferenceException’ avec le message ‘Object reference not set to an instance of an object.’.

**Full stack trace :**

System.NullReferenceException: Object reference not set to an instance of an object.  
at SonarAnalyzer.Wrappers.ObjectCreationFactory.ImplicitObjectCreation.TypeAsString(SemanticModel semanticModel)  
at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)  
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)  
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)  
at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)  
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)  
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)  
at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)  
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)  
at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.VisitAssignmentExpression(AssignmentExpressionSyntax node)  
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)  
at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)  
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)  
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)  
at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)  
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)  
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)  
at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)  
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)  
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)  
at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)  
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)  
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)  
at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)  
at SonarAnalyzer.Helpers.SafeCSharpSyntaxWalker.SafeVisit(SyntaxNode syntaxNode)  
at SonarAnalyzer.Rules.CSharp.UnusedPrivateMember.\<\>c\_\_DisplayClass24\_0.b\_\_1(SyntaxReference x)  
at System.Linq.Enumerable.All[TSource](IEnumerable`1 source, Func`2 predicate)  
at SonarAnalyzer.Rules.CSharp.UnusedPrivateMember.NamedSymbolAction(SonarSymbolReportingContext context, HashSet`1 removableInternalTypes) at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [November 23, 2023, 7:51am UTC](https://community.sonarsource.com/t/sonaranalyzer-rules-csharp-unusedprivatemember-exception/104771/4 "2023-11-23T07:51:03Z")

</div>

Thanks y’all for the feedback. I’ve pinged the right team.

---

<div class="post-metadata">

**Author:** ![sebastien.marichal](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/sebastien.marichal/32/69045_2.png) [@sebastien.marichal](https://community.sonarsource.com/u/sebastien.marichal)\
**Post date:** [November 27, 2023, 12:47pm UTC](https://community.sonarsource.com/t/sonaranalyzer-rules-csharp-unusedprivatemember-exception/104771/6 "2023-11-27T12:47:32Z")

</div>

Hello @milbrandt, @PTR,

Prior to version 9.13 of the C# Analyzer, we do not support C# 12 new syntaxes, such as [Collection Expression](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/collection-expressions):

```cs
SKPoint[] points = [new(start.X, start.Y), new(end.X, end.Y)];

```

Both SonarLint 7.4 and 7.3 embed a version that does not support C# 12 new syntaxes

@PTR, I am assuming your issue is also C# 12 related. If this is not the case, could you provide a reproducer?

At the moment both 9.13 and 9.14 versions are not available from NuGet due to technical issues during publishing. Hopefully, 9.14 will be published and available soon.  
It might take a bit more time for the next version of SonarLint to be released with the latest version of the C# analyzer.

However, both SonarQube (10.3) and SonarCloud are compatible with C# 12.

Sorry for the inconvenience.
