1

I am a new to C# and .NET 9. I am trying to execute the following program I found from the official MS tutorial - Create record types

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}");
}

in my VS Code. When I run dotnet build, I get the following error :

Restore complete (1.1s)

TestProject failed with 2 error(s) (1.0s)
/workspaces/workspace/c#/CsharpProjects/TestProject/Program.cs(3,1): error CS8803: Top-level statements must precede namespace and type declarations.
/workspaces/workspace/c#/CsharpProjects/TestProject/Program.cs(3,1): error CS0106: The modifier 'public' is not valid for this item

Build failed with 2 error(s) in 3.5s

However, the same program available in the web tutorial works fine when I click on run (green button on top right).

I would like to know why it does not work locally? And how can I fix it?

4

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

5

Either:

  1. Put your Main method inside a class
  2. Move the code from Main out of the method

You're confusing it by having a method not inside a type, as to whether you're trying to use top-level statements or not. Top-level statements replace the need for a Main method as an entry-point, but impose some additional rules. It is fine to use the older Main approach, but that needs to be declared inside a class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.