Home > Software engineering >  How to set variables for `dotnet new`?
How to set variables for `dotnet new`?

Time:09-27

Standard template for command

dotnet new console

contains #if for the preprocessor:

#if (csharpFeature_TopLevelProgram)
// See https://aka.ms/new-console-template for more information
#endif
#if (!csharpFeature_ImplicitUsings)
using System;

#endif
#if (csharpFeature_TopLevelProgram)
Console.WriteLine("Hello, World!");
#else
namespace Company.ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}
#endif

What command line switches should I specify to dotnet new so that the variable "csharpFeature_TopLevelProgram" is not defined and so that when I execute dotnet new console the file is generated not for Net6 but without TopLevelProgram as before in the good old Net5?

CodePudding user response:

If you use the -h command line option, it tells you how to use the template and that you can specify the --use-program-main parameter:

enter image description here

For example:

dotnet new console --use-program-main true

Note: You may need to update to a newer version of .NET 6 for this command line option to exist. This was tested with v6.0.401

CodePudding user response:

My dotnet 6.0.108 has no option --use-program-main true to create initial program in no top level style but dotnet 6.0.401 has. The issue is closed.

  • Related