Microsoft Learn sample C# code issue on .NET 10.0 [duplicate]

1 week ago 10
ARTICLE AD BOX

You should put the Main method inside a class, or use top level statements, i.e. move the body of the Main method before the record declaration.

Point pt = new Point(1, 1); var pt2 = pt with { Y = 10 }; Console.WriteLine($"The two points are {pt} and {pt2}"); public record Point(int X, int Y);

The reason why you were able to run the same code on the Microsoft Learn website, is because that code is actually part of a larger file that you can't see.

The interactive code block on the website is powered by Try .NET, which is a tool that turns .md files into interactive sites with code blocks directly runnable in the browser.

The original .md file is here. In it, it references a PointEvolution.cs file like this:

:::code language="csharp" interactive="try-dotnet-class" source="./snippets/TuplesAndTypes/PointEvolution.cs" id="PointVersion2":::

Try .NET converts this into that interactive code block you see. PointEvolution.cs actually contains:

namespace PointEvolution; public static class SampleTwo { // <PointVersion2> public record Point(int X, int Y); public static void Main() { Point pt = new Point(1, 1); var pt2 = pt with { Y = 10 }; Console.WriteLine($"The two points are {pt} and {pt2}"); } // </PointVersion2> }

Only the part in the <PointVersion2> tag is visible and editable on the website. The code you see on the website is actually enclosed in a separate class.

Read Entire Article