#13 – Specify Command Line Arguments in Visual Studio 2010

You’d normally pass command line arguments to your program when you invoke it from the command line.  However, for testing purposes, you may want to specify command line arguments that get passed to the program when you run it from the debugger.  You can do this in the Project Properties window, on the Debug tab.

Note that:

  • You can do this for all project types, e.g. console app, Win Forms, WPF
  • You can specify different arguments for Debug vs. Release mode
  • These arguments will be used only when running from the debugger

#12 – Read Command Line Arguments

If you are calling your C# program from the command line, you can pass in one or more command line arguments.  In your program, you can access these arguments by defining the Main function to accept a string array as a parameter and then iterating through the array to read the parameters.

static void Main(string[] args)
{
    // Ex 1: List all arguments
    foreach (string arg in args)
        Console.WriteLine(string.Format("Arg: [{0}]", arg));

    // Ex 2: Use 1st argument as filename
    if (args.Length > 0)
        Console.WriteLine(string.Format("File={0}", args[0]));
}

#11 – Examine IL Using Ildasm.exe

In .NET, source is compiled to an platform-neutral intermediate language called Common Intermediate Language.  The IL is later compiled into native code at runtime, running in a virtual machine.

You can examine the IL for your applications by running a tool called the IL Disassembler (ildasm.exe).  You can find ILDasm in a directory that looks something like this:

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\x64

Alternatively, you can just type “ildasm” in the Start Menu search box in Windows 7 or Windows Vista.

ILDasm will show you three basic things:

  • A list of all classes and methods in your assembly
  • The contents of the assembly’s manifest
  • IL code for any method implemented in the assembly.

Here’s an example of what is displayed in ILDasm.

#10 – The Return Value from Main() Sets ERRORLEVEL Variable in Windows

If your Main() function returns an integer value, you can check that value in the calling program or script using the ERRORLEVEL environment variable.  By convention, a return value of 0 indicates success and any other value indicates an error.  Below is a sample .bat file that calls a program called MyProgram and then checks the return value.

@echo off

MyProgram

IF "%ERRORLEVEL%" == "0" goto OK

:NotGood
    echo Bad news.  Program returned %ERRORLEVEL%
    goto End

:OK
    echo Everything A-OK

:End

#9 – Main() Should Not Be Public

The following code will compile, but is not recommended, according to the MSDN documentation. If Main() is public, other classes could call it.  But this violates the intent for Main(), which is meant to only be called once, when the program is invoked.

class Program
{
    static public void Main()
    {
        Console.WriteLine("This would compile..");
    }
}

#8 – The Main() Function Can Return an int

Instead of returning void, Main() can return an integer value, which can then be read by the calling program or script.  Notice that the return type of the function is an int

class Program
{
    static int Main()
    {
        Console.WriteLine("Doing something..");

        if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)
            return -1;     // Monday is bad
        else
            return 0;
    }
}

#7 – Reading and Writing from the Console

To read and write from the console, use Console.Read, Console.ReadLine, Console.Write, Console.WriteLine.

namespace ReadWriteConsole
{
    class Program
    {
        static void Main()
        {
            Console.Write("This ");
            Console.WriteLine("is all on one line.");
            Console.Write("Enter your name: ");
            string name = Console.ReadLine();
            Console.WriteLine(name);
            Console.Read();     // pause
        }
    }
}