Mysterious `NullReferenceException` when initializing a `List<int>`

Open
#109,296 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
devtools

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

area-Diagnostics-coreclr tracking-external-issue
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.

Image

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
  1. Create a new .NET project e.g Console App, IDEs should not matter, I'll do it in Rider

  2. 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)
        {
        }
    }
}
  1. Put a break point at line 4, at the start of the Main method
    Image

  2. Start the app in Debug mode

  3. When arriving at the break point, jump into the try block to line 6
    Image
    Image

  4. Step through the method with F10

  5. Observe that a NullReferenceException is thrown at the line var list1 = new List<int>();
    Image

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.