-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Confusing NullReferenceException on default ImmutableArray #98300
Comments
Tagging subscribers to this area: @dotnet/area-system-collections Issue DetailsI was recently writing an Analyzer, and I was working with an ImmutableArray, iterating through it using LINQ to grab values. My analyzer was then crashing when another project was using it, and there was no stacktrace to go off, just the exception type and message, which was this: Warning AD0001 : Analyzer '' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.' After some digging, I eventually found out that it's because I was being passed a Now that's fine, but what was so confusing was the NullReferenceException. Because of the lack of stacktrace, I couldn't see where this was happening. So first thing was to check my code for any Nullability warnings, or double check that I'm not suppressing any somewhere. I had no null warnings, and no null suppressions. This made it super difficult to trace down. Would it not make sense to throw a better exception, such as ImmutableArrayNotInitializedException? It feels like if any code can throw a NullReferenceException, then the compiler should be able to tell us that via the nullability warnings. This however is an exception.
|
Performance is the key characteristic of This is indeed some quirk that you need to remember. An analyzer flagging |
It is a known trade-off in immutable array design. As of C# 12 using collection literals should provide a simpler way to declare valid empty instances. Closing in favor of dotnet/csharplang#146 |
I was recently writing an Analyzer, and I was working with an ImmutableArray, iterating through it using LINQ to grab values.
My analyzer was then crashing when another project was using it, and there was no stacktrace to go off, just the exception type and message, which was this:
Warning AD0001 : Analyzer '' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'
After some digging, I eventually found out that it's because I was being passed a
default
ImmutableArray, which can't be iterated through.Now that's fine, but what was so confusing was the NullReferenceException.
Because of the lack of stacktrace, I couldn't see where this was happening. So first thing was to check my code for any Nullability warnings, or double check that I'm not suppressing any somewhere.
I had no null warnings, and no null suppressions.
This made it super difficult to trace down.
Would it not make sense to throw a better exception, such as ImmutableArrayNotInitializedException?
It feels like if any code can throw a NullReferenceException, then the compiler should be able to tell us that via the nullability warnings. This however is an exception.
I know I can check the Is default property, which is what I'm now doing, but this is more to highlight the issue with going against the norm of nullability warnings via the compiler.
The text was updated successfully, but these errors were encountered: