Hello World, the quick version from the command-line using Notepad++

Hello World, the quick version from the command-line using Notepad++

When I first started my exploration into software development, I went through the Hello World examples across each platform I was learning.  Being the geek that I am… I enjoy learning from the command-line because it takes away the complexity and distractions of the Integrated Development Environment (IDE) or being side-track by the cool features of the interface that may take-away from me learning the raw code.

For writing CSharp (C#) code from the command-line, Notepad++ is very useful!

Download and install Notepad++, when that’s done you are ready to begin.

Open Notepad++ with a new document:  File > New.

Change the Language from the menu: Language > C > C#.  This will help with formatting although it will not let you take advantage of the features of Visual Studio’s Intellisense.

Your first line of code will be a reference to the System library in the Microsoft .NET framework.  To reference it you simply stat with the using statement and the destination of the library.  At the end of the link you terminate it with a semi-colon (;).

using System;

Next is to build the class that will hold the instructions for the application.  The basic structure of the class only requires the type of object being built (class) and the name (HelloWorld) encased in curly braces ( { and } ) which designates the start and end of the code section.

class HelloWorld

{

    // Method will be placed here.

}

 

In order for the application to run or do something we must declare a Main method that will run.  This is the method that the console application will look for when it starts.

Inside of the HelloWorld class, insert the Main method:

static void Main()

{

    //  Program will go here…

}

 

First you will notice the static keyboard being used, this declares a static member that exists but cannot be used by other indexers, destructors, or types other than classes. (More on that much later).  The void keyword is important here because it lets the application know that it will not be waiting for a response or value to be returned so just run it.  Then we create the name Main and follow it with parenthesis to identify it as a method.

The double forward slashes (//) are used to comment out messages that help other identify where and what the application does.  So underneath that we will add our program commands.

What should this application do?  Well since this is a console application and we are already targeting the System library, we will use the System.Console class to access a method called WriteLine.  The specific method allows us to pass a string that will be displayed on the screen. We write the command with the method and the string value enclosed in quotes (“) and terminate the command with a semicolon (;).

Console

.WriteLine(“Hello World!”);

Your completed application in CSharp should look like this:

 

using System;

class Test
{
// Method will be placed here.

    static void Main()
    {
    //  Program will go here…
    Console.WriteLine("Hello World!");
    }

}

You’re all done, save the application to your computer as a CSharp file HelloWorld.cs.  Now open the Command Prompt and navigate to that folder where you save the file.

Type in the location of the CSharp compiler (CSC.exe) that is in your Microsoft .NET folder.  If you type C:\Windows\Microsoft.Net\ at the command line and hit TAB you should be able to find it.  A quick way to navigate a path from the command line it to use TAB as follows:

C:\Windows\Microsoft.Net\Framework\v3.5\csc.exe
Typing the following –> C:\Windo (TAB), \ , Micros (TAB), \ , Fram (TAB), \, v (TAB until you find v3.5), \, csc (TAB).

If you have another framework installed such as v2.0.50727, v3.5, v4.0.30319, feel free to use it since the compiler will work because the Console.WriteLine in the System library is the same for all versions.

You’re command prompt should now have the complete path laid out so you can hit space and provide the csharp file you created HelloWorld.cs and hit enter.

C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe HelloWorld.cs  [HIT ENTER]

Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.5420
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

If everything goes well you will not see any errors and you will find a HelloWorld.exe file in the folder.  Run it!