Repository: dotnet/roslyn
File: src/Compilers/CSharp/Test/Semantic/Semantics/ForEachTests.cs (C#)

Code before:
        const object NULL = null;
        foreach (int x in NULL)
        {
        }
    }
}";

            CreateCompilation(text).VerifyDiagnostics(
                // (7,27): error CS1579: foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public instance or extension definition for 'GetEnumerator'
                //         foreach (int x in NULL)
                Diagnostic(ErrorCode.ERR_ForEachMissingMember, "NULL").WithArguments("object", "GetEnumerator").WithLocation(7, 27)
                );
        }

        [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")]
        public void ForeachOnDefaultArray()
        {
            var source = """
                class C
                {
                    static void Main()
                    {
                        foreach (int x in default(int[]))
                        {
                        }
                    }
                }
                """;

            CreateCompilation(source).VerifyEmitDiagnostics();
        }

        [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")]
        public void ForeachOnDefaultString()
        {
            var source = """
                class C
                {
                    static void Main()
                    {
                        foreach (char c in default(string))
                        {
                        }
                    }
                }
                """;

            CreateCompilation(source).VerifyEmitDiagnostics();
        }

        [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")]

Review comment (suggestion):
>{ [](http://example.com/codeflow?start=28&length=1)

I think it will be useful to observe that the loop didn't execute,  consider printing something from the loop, or throw a different exception.

Code after:
        const object NULL = null;
        foreach (int x in NULL)
        {
        }
    }
}";

            CreateCompilation(text).VerifyDiagnostics(
                // (7,27): error CS1579: foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public instance or extension definition for 'GetEnumerator'
                //         foreach (int x in NULL)
                Diagnostic(ErrorCode.ERR_ForEachMissingMember, "NULL").WithArguments("object", "GetEnumerator").WithLocation(7, 27)
                );
        }

        [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")]
        public void ForeachOnDefaultArray()
        {
            var source = """
                class C
                {
                    static void Main()
                    {
                        try
                        {
                            foreach (int x in default(int[]))
                            {
                            }
                        }
                        catch (System.NullReferenceException)
                        {
                            System.Console.Write("NullReferenceException");
                        }
                    }
                }
                """;

            CompileAndVerify(source, expectedOutput: "NullReferenceException").VerifyDiagnostics();
        }

        [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")]
        public void ForeachOnDefaultString()
        {
            var source = """
                class C
                {
                    static void Main()
                    {
                        try
                        {
                            foreach (char c in default(string))
                            {