If you install the Visual Code and start a new project, you may see this on the Program.cs file.
1 2 | // See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!"); |
It is a new feature to speed your start development step.
Using the command dotnet run will work well.
This feature is enabled in your csproj file type and will include all you need to run the source code, like using System and all of these: namespace … void Main …
1 2 3 4 5 6 7 | <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> ... <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup> </Project> |
To disable and return to the old-style source code:
1 2 3 4 5 6 7 8 9 10 11 12 | using System; namespace MyApp // Note: actual namespace depends on the project name. { internal class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); } } } |
… use this in the csproj file project:
1 | <ImplicitUsings>disable</ImplicitUsings> |
For example, the console applications come with these directives implicitly included in the application when you use this feature:
1 2 3 4 5 6 7 | using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks; |
If you want to select and restrict some of these you need to add them in the csproj file project.
For example, to run a console application for using only the System and NumSharp packages with this feature enabled, you need to have this on your csproj file project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="NumSharp" Version="0.30.0" /> </ItemGroup> <ItemGroup> <Using Include="System" /> <Using Remove="System.IO" /> <Using Remove="System.Collections.Generic" /> <Using Remove="System.Linq" /> <Using Remove="System.Net.Http" /> <Using Remove="System.Threading" /> <Using Remove="System.Threading.Tasks" /> </ItemGroup> </Project> |
You can read more about this feature on this webpage.