Home arrow C# arrow Page 2 - The Basics of C#: A "HelloWorld" Application
C#

The Basics of C#: A "HelloWorld" Application


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.

Author Info:
By: James Yang
Rating: 4 stars4 stars4 stars4 stars4 stars / 41
January 09, 2002
TABLE OF CONTENTS:
  1. · The Basics of C#: A "HelloWorld" Application
  2. · Hello World Example
  3. · HelloWorld source code explained
  4. · Simple C# language features
  5. · Conclusion

print this article
SEARCH DEVARTICLES

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:

csc helloworld.cs

To execute our application, simply type:

Helloworld

If everything goes well, your screen should look like this:

Compiling and executing our hello world application

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.


blog comments powered by Disqus
C# ARTICLES

- Introduction to Objects and Classes in C#, P...
- Visual C#.NET, Part 1: Introduction to Progr...
- C# - An Introduction
- Hotmail Exposed: Access Hotmail using C#
- Razor Sharp C#
- Introduction to Objects and Classes in C#
- Making Your Code CLS Compliant
- Programming with MySQL and .NET Technologies
- Socket Programming in C# - Part II
- Socket Programming in C# - Part I
- Creational Patterns in C#
- Type Conversions
- Creating Custom Delegates and Events in C#
- Inheritance and Polymorphism
- Understanding Properties in C#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials