I recently tried the .Net 6 console template, with top level statements, in Visual studio and stumbled into a Gotcha. If you try to compile the below code, Visual Studio will give a red squiggly line under the string declaration var s = "myString";
. You will also receive the error: Top-level statements must precede namespace and type declarations.
delegate string StringReturner(int i);
var s = "myString";
Console.WriteLine(s);
So, what is the problem here?