Resolved Pre Formatted Main() Program Template In VS?

Dalski

Active member
Joined
Jun 3, 2020
Messages
40
Programming Experience
Beginner
Standard C# Doc:
using System;

class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World!");
    }
}
How does one automatically generate this code in Visual Studio? Surely there is an auto-filetype that one can select? Don't want to create a snippet, must be a standardized way of generating this quickly.
 
Thanks jmcilhinney, that's really suprising, of the many superfluous templates provided there isn't one for main; I am shocked
o_O.
 
When you create a new application project the Program.cs with Main method is generated.
 
Part of the minimalist approach Microsoft has taken with the default templates is from its learnings in the past from developer feedback regarding the older templates Microsoft used to provide for the older Win32, MFC, and ATL project types: they used to overwhelm beginners because they used to provide too much; and at the same time annoy professionals because they would just delete everything and start from scratch. The only people who liked the bigger templates where PMs and technology evangelists who had to do quick demos. Hence the scaled back template contents: enough to show something if you pressed F5, but not so much crap that needed to be replaced anyway in a production ready project.
 
I'm slightly confused about the issue here. I just created new C# Console Application projects targeting .NET Framework and .NET Core and got this:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
and this:
C#:
using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
respectively. What's your problem with those? Neither is exactly what you specified so you can make the required modifications and export your own template. I've never found either of those deficient though.
 
Thanks guys, I'm just surprised that it's not in the main set of templates that's all. But that being said I'm using the IDE in an unusual manner. I'm only a beginner so bear with me.

I want all my tutorials/ reference in a single place So I'm going through tutorial by tutorial, creating main functions for each tutorial & saving these in their individual topics. When I've finished I exclude the file from the build process. Create another new file (with a main function) & repeat the process for that tutorial. It's great because I have all topics of each tutorial in a single place; & I'm loving it.

When I need to refresh on a particular topic I turn that file on in the build process & brush up on the particular topic in hand.
 
My first thought on this is that source control would be a better solution. Just go back to a point in time where you have the boilerplate you want, and then branch from there. Use git, Mercurial, Subversion, TFS, etc. This doesn't tie you to a particular IDE.

An alternative is that in Visual Studio, a single solution can have multiple projects. It's a simple matter of changing the startup project for which ever tutorial you want to work on. And you get to take advantage of the project templates mentioned above, when you create a new project.
 
Thanks guys, I'm just surprised that it's not in the main set of templates that's all. But that being said I'm using the IDE in an unusual manner. I'm only a beginner so bear with me.

I want all my tutorials/ reference in a single place So I'm going through tutorial by tutorial, creating main functions for each tutorial & saving these in their individual topics. When I've finished I exclude the file from the build process. Create another new file (with a main function) & repeat the process for that tutorial. It's great because I have all topics of each tutorial in a single place; & I'm loving it.

When I need to refresh on a particular topic I turn that file on in the build process & brush up on the particular topic in hand.
Ah, OK. That sheds some light on the request. I didn't realise that you were asking about adding multiple Main methods to an existing project. Why would VS provide help to do that? It's not something that you would ever want to do in a "real" project. An application can only have one entry point and that is created automatically when you create the project, which is what I assumed you were talking about. I can't imagine any reason that adding more Main methods to a project would be something that occurred often enough that Microsoft would build it into VS. They do provide a way for you to create your own reusable code blocks but you have, for some unknown reason, decided not to use that tool that seemingly does exactly what you want. I can't think why they'd provide another tool to do something that is almost never done but could be done with little effort using the existing tool.

You could also simply save a code file containing the desired code and add it as an existing item to any project at any time.
 
Back
Top Bottom