ASP.Net Core Top-level Statements

Kenneth R. Jones 46 Reputation points
2024-05-04T11:00:37.9866667+00:00

I understand what this option does, but I'm wondering if this only applies when you first create a project. I can't find any option to disable top-level statements after the project has been created, and I can't see that VS stores this option. I would assume the most logical place would be in project settings, but I can't find the option.

I did not use this option when I created the project, and I would like to change it, because I find the top-level statements to be very confusing. Do I need to go through and manually edit each file to remove the top-level statements?

tls

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-05-04T16:45:28.81+00:00

    You are picking options for the template that creates the project, not project properties. Once the project is created, you need to manually make changes. Say convert back to using Main. note: support for optional top level is a compiler feature, and is based on the format of the file, not project settings.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2024-05-04T11:37:00.4733333+00:00

    I can't find any option to disable top-level statements after the project has been created, and I can't see that VS stores this option.

    There is nothing to disable. Top level statement is a compiler feature with C# 10. It is totally up to you if you want to use top level statement or not.

    https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/top-level-statements

    If you are referring to the the implicit using statement then see the following link.

    https://learn.microsoft.com/en-us/dotnet/core/tutorials/top-level-templates#disable-implicit-using-directives

    Do I need to go through and manually edit each file to remove the top-level statements?

    Yes. As far as I know there is no conversion utility that changes code from a top level style to code blocks with curly brackets, a Program class and Main() method in the project file.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.