Mysterious `NullReferenceException` when initializing a `List<int>`
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Reproduce the case from Program.cs using the shown List, LINQ expression, try-catch, and debugger stepping sequence in Debug mode on .NET 6 or 8. Compare breakpoint jumping and F10 behavior with Release mode, then investigate the debugger/runtime path involved. Done means the reproduction no longer reports a NullReferenceException while preserving the expected behavior.
Written by the indexing model from the issue text.
Description
Description
I encountered a NullReferenceException when initializing a list inside a try-catch block in Debug mode.
The exception is thrown at the line where the list is initialized, which should not normally cause a NullReferenceException.
Additional Information
- The issue does not occur in Release mode
- The issue does not occur when stepping through the code with the debugger using F10, only when jumping into try-catch
- The issue persists across different machines and IDEs (Visual Studio, VS Code, Rider).
- The issue persists in .NET6 and .NET8 (have not tried other versions)
- Disabling compiler optimizations in Debug mode does not resolve the issue
- Removing the try-catch will prevent the exception
- Removing the LINQ expression will prevent the exception
Reproduction Steps
-
Create a new .NET project e.g Console App, IDEs should not matter, I'll do it in Rider
-
Add the following code the Program.cs
class Program
{
static void Main()
{
try
{
var list1 = new List<int>();
var list2 = new List<int>();
list1.Where(x => list2.All(x2 => x == x2));
}
catch (Exception e)
{
}
}
}
-
Put a break point at line 4, at the start of the
Mainmethod
-
Start the app in Debug mode
-
When arriving at the break point, jump into the
tryblock to line 6
-
Step through the method with F10
-
Observe that a
NullReferenceExceptionis thrown at the linevar list1 = new List<int>();
Expected behavior
No exceptions, just as in release mode.
Actual behavior
Throws mysterious exception.
Known Workarounds
Change the LINQ expression to traditional loops.
Configuration
- NET SDK: NET6,8
- Operating System: Win10, 11
- Architecture: Any CPU, x64
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area-System.Reflection blocking-clean-ci-optional Known Build Error os-mac-os-x untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
area-CodeGen-coreclr untriaged
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
agentic-workflows untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
area-VM-meta-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
Create parent directories only after the containment check in InstallHelper.TryExtractToDirectory Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PowerShell/PSResourceGet#2056 ·