C# is part of Microsoft's evolutionary .NET environment. It has collected the best features and aspects of many other programming languages including Visual Basic and C++. In this article James will show us how to create and compile a simple "HelloWorld" C# console application.
The Basics of C#: A "HelloWorld" Application - Hello World Example (Page 2 of 5 )
Let's start by creating a very simply "Hello World" C# console application. Our app will simply display the text "Hello World" on the screen whenever it is executed. A console application refers to any application that runs in a command shell within the Windows environment. Using the .NET framework, we can also create windows applications, web services, and components.
The purpose of developing this simple application is to give you a rough idea of the structure, syntax, look and feel of a C# application. I will also explain how to compile the application once we’ve created it.
One of the great things about .NET is that you can create your source code files in any text editing application that you like; you’re not confined to using the Visual Studio.NET IDE (although it does make it easier when developing windows applications). Open notepad and create a new file named "helloworld.cs". Copy-paste the following code into helloworld.cs:
using System; // Calling Namespace
namespace helloworld // Defining Namespace
class MainClass // Defining Class
static void Main(string args) // Program Entry Point
Console.WriteLine ("Hello World"); // Print Hello World
Save the file into c:\, and then minimise notepad. Now that we've created our source code file, we have to compile it using .NET's built-in C# compiler, "csc.exe". It's important to note that the C# compiler won't actually compile our application into machine code; it compiles it into MSIL instead. Microsoft Intermediate Language (MSIL) is a CPU-independent set of instructions that can be effectively converted to native code. MSIL is similar to the code produced by Java compilers; the code closely resembles machine code, however it still has one more stage of complete compilation before it can be properly executed. Compilation to MSIL provides numerous benefits for anyone using .NET applications:
When MSIL code is compiled by the .NET compiler, it will optimise the code to perform at its highest potential for the machine onto which it is being installed. This means that for example, if I am running a Pentium 4, then the machine code produced by the compiler will be aware of this, and will be optimised as a result.
Because MSIL isn't compiled for any specific CPU or operating system, cross-platform .NET applications are a definite possibility. This is one of the reasons why Java was so popular. Java's slogan "Write Once, Run Anywhere" is theoretically possible, however a lot of Java developers have found it hard to port code to another O/S using JVM. Although at this point in time, no companies have started working on a .NET compiler for other operating systems, there's a good chance that someone will soon.
It doesn't matter which language you use to create your applications with, they are all compiled down to one common language before they are finally compiled into machine code: MSIL.
Open a new command prompt window and change into the directory where you saved "helloworld.cs" into. Enter the following command to compile our C# source code file:
To execute our application, simply type:
If everything goes well, your screen should look like this:
Congratulations, you have just created your first working C# console application! Now that we know how to create, compile and execute a C# application, let's take a look at a detailed explanation of our C# source code file.