Alex Rivera | Logout

Minimal, good-citizen, C# console application boilerplate

Asked 2009-10-27T14:48:08.967
24

What would be the minimal boilerplate code for a C# console application entry-point that would make it a well-behaved citizen?

When anyone goes out to create a Console Application project using Visual Studio (up to 2008 at the time of writing), you are presented with a boilerplate Program.cs that looks like this:

class Program
{
    static void Main(string[] args)
    {
    }
}

There are, however, a few things that everyone needs to do to make a console application a minimally good citizen. For example, if an exception occurs, then write out a clean message to standard error (Console.Error) and not standard output (Console.Out). Likewise, set the error code to a non-zero value in the event of some error so that calling processes can detect failures.

What would be the minimal boilerplate code for a C# console application entry-point that would make it a well-behaved citizen? What would you add or change to the following?

using System;
using System.Diagnostics;
using System.Linq;

static class Program
{
    static void Run(string[] args)
    {
        // TODO Replace line-echoing sample with actual application code

        string line;
        while ((line = Console.ReadLine()) != null)
            Console.WriteLine(line);
    }

    static int Main(string[] args)
    {
        // TODO Use a more robust arguments parser
        if (args.Any(arg => arg.Equals("/v") || arg.Equals("-v"))) // verbose?
            Trace.Listeners.Add(new ConsoleTraceListener(true));

        try
        {
            Run(args);
            return Environment.ExitCode;
        }
        catch (Exception e)
        {
            Console.Erro
Edit
Report

1 Answer

0

I use emacs, and a thing called defaultcontent.el . If you aren't familiar, Emacs is a text editor, and is very extensible. defaultcontent.el is an extension that inserts (surprise) default content into new files, when they are first created by emacs.

So if I try to open a .cs file that does not exist, emacs creates it, and then puts a bunch of default content into it. I decide what content to include in new files. For my C# files, this content includes:

  • simple documentation with the timestamp, copyright, other boilerplate.
  • a bunch of assembly-scope attributes (FileVersion, AssemblyDescription, etc)
  • a single class
  • a contructor that accepts an array of string arguments, and parses them; the parse logic is a switch statement, with some boilerplate for parsing integers, strings, and booleans.
  • a Usage method, that prints the usage information.
  • a Main method, which instantiates the class and calls "Run()"; this is surrounded in a try..catch and in the catch, the Usage() method is called. If any error occurs anywhere in the program, the usage message is displayed.

The defaultcontent.el also lets me position the cursor where I want. In my case, that's in the middle of the empty Run() method.

This is my default content:

// default.cs
// ------------------------------------------------------------------
//
// Description goes here....
// 
// Author: MyUser
// built on host: MyMachine
// Created Tue Oct 27 15:01:18 2009
//
// last saved: 
// Time-stamp: <2009-October-20 00:18:52>
// ------------------------------------------------------------------
//
// Copyright Notice here
// All rights reserved!
//
// ------------------------------------------------------------------

using System;
using System.Reflection;


// to allow fa
answered 2009-10-27T19:09:18.610

Your Answer