Is it possible to disable the new "top-level-statements" compiler configuration, which obfuscates and makes the language less transparent?

Jim Copeland 20 Reputation points
2024-09-17T13:35:30.63+00:00

It makes no sense to have top-level statements - it would have been better to have the console app auto-generate the boiler-plate console app with a Main() method, the same way a boilerplate HTML file can be created quickly. The language was consistent and rational before. Now it is more opaque to new programmers - exactly the opposite of whatever was intended, I'm sure.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,962 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 54,401 Reputation points
    2024-09-17T14:35:17.85+00:00

    Top level statement is not an option that you turn on or off. You just don't use it. You can add the standard boilerplate code (using, main, etc) in the main file. It is strictly an optional feature that you either use or not (like a lot of compiler features like var and switch expressions). When you create a new project there is a checkbox you can check to not use top-level and it generates the initial code without it. But, again, this is completely optional and there is nothing to turn on or off.

    The implicitusings setting has nothing to do with top-level statements. What it does is enable the using of implicit usings in your C# code. By default you don't need to using system because the compiler automatically brings it in and has since the earliest days of the language. But as more and more functionality is moved to other namespaces then the # of additional usings you have to have in your code explodes. To help cut down on all the extra usings you can define per-project implicit usings. If there is an implicit using in a project then you don't need to have a using for that namespace in any of your files.

    This is a compiler option because if it is set then the NET SDK will add a series of common system namespaces to the implicit usings such as system, System.Collections.Generic, etc. If you are building framework apps like Winforms or WPF then the additional SDKs can add additional namespaces.


2 additional answers

Sort by: Most helpful
  1. Adharsh Santhanam 4,135 Reputation points
    2024-09-17T13:48:23.4466667+00:00

    Hello Jim Copeland, yes you can disable the top-level statements feature in your C# projects if you prefer the traditional structure with explicit Main methods. To do so, add <ImplicitUsings>disable</ImplicitUsings> to your project file in the <PropertyGroup> element as shown below.

    <Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>

    ...
    
    <ImplicitUsings>disable</ImplicitUsings>
    

    </PropertyGroup>

    </Project>

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.


  2. Jim Copeland 20 Reputation points
    2024-09-17T13:57:30.1633333+00:00

    Nevermind - the checkbox doesn't do it

    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.