#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
        }
    }
}

#6 – An Even Smaller C# Program

In The Smallest Possible C# Program, I mentioned a couple things as optional.  For the record, here’s the absolute smallest C#.NET program that you can write.  (Assuming that you don’t need it to actually do anything).

class Program
{
    static void Main()
    {
    }
}

#5 – How is C# Different from VB.NET?

C# and Visual Basic .NET are both first-class languages in Microsoft’s .NET programming environment.  They both are typically run on top of Microsoft’s .NET Framework.

Most of the differences between the two languages are merely syntactic.  But there are a few meaningful differences including (but not limited to):

C# Has (VB.NET Doesn’t)

  • Can write unsafe code
  • Static classes
  • checked/unchecked
  • Auto-implemented properties
  • Case-sensitivity
  • Stricter type checking
  • Less verbose

VB.NET Has (C# Doesn’t)

  • XML literals
  • Inline date declarations
  • IsNumeric
  • Marshalling an object for multiple actions
  • Auto wire-up of events

See also: Comparison of C# and VB.NET

#4 – How is C# Different From Java?

C# and Java are both object-oriented languages that derive their syntax from C and run in a managed environment.  There are, however a number of differences.  Here are the main ones:

  • Syntactic differences, e.g. “class B extends A” instead of “class B : A”
  • Java doesn’t use namespaces
  • C# lock statement vs. Java synchronized statement
  • C# has a few more access modifiers than Java
  • In Java, enumerated types are full-fledged classes
  • C# allows strings in switch statements
  • C# programs make use of the .NET Framework; Java programs use the Java SE

Lots more at: C# From a Java Developer’s Perspective