Note that I am still editing this post and I will be adding platforms over time. But in an effort to get even more content out there for the community it’s going to be bits and pieces over time. When it’s all said and done I want to have the Hello World application using the console, Windows Forms, WPF, ASP.NET, ASP.NET MVC, and Silverlight.
So keep checking back, sign up for our RSS feed, and keep sending new programmers this way!
PREREQUISITES
OVERVIEW OF THE QUINTESSENTIAL HELLO WORLD
Hello World is the most common programming example – anywhere. But one thing I’ve yet to see is the quintessential hello world. You know, the one that not only exemplifies it in a console application, but also exemplifies it in many other platforms. Well today you’re in luck because that’s exactly what I’m going to do.
Before we get into the code let’s get a set of specifications together so we know what we’re building. The customer, ACME, wants us to build an application that will tell the user “Hello” when they open it up – and since they don’t want the user to login – they just want it to say “Hello World!”
THE CONSOLE APPLICATION
The console application has got to be the most common example – anywhere – but for this to be the quintessential hello world we are going to have to include it.
- Open Visual Studio Express 2012 for Windows Desktop.
- Create a new project and select the Console Application template.
- Name the project TheQuintessentialHelloWorld and click OK.
Once you click OK you’ll notice that Visual Studio creates a project and seeds it with the proper files for a basic Windows console application.
Open up the Program.cs file, and add the following code to the Main method:
Console.WriteLine("Hello World!");
if (Debugger.IsAttached)
{
Console.ReadLine();
} |
so when you’re done your program should look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| using System;
using System.Diagnostics;
namespace TheQuintessentialHelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
if (Debugger.IsAttached)
{
Console.ReadLine();
}
}
}
} |
Press F5 and you’ll notice Visual Studio doing some things, and then a console window will popup, and it should say Hello World!
Congratulations! You just created the Hello World application using the Windows console!
THE WINDOWS APPLICATION
So the Windows application is probably the next most common example, so to ensure that this is in fact the quintessential hello world post we are going to have to include it – but we’re going to have to +1 all of the others out there. We’re going to do it for both Windows Forms and WPF.
WINDOWS FORMS
- Open Visual Studio Express 2012 for Windows Desktop.
- Create a new project and select the Windows Forms Application template.
- Name the project TheQuintessentialHelloWorld_WF and click OK.
You’ll notice that Visual Studio has created a new project and there is a file in there named Form1.cs. You’ll also notice there is a visual designer in front of you with the Form1.cs file already open. Now there’s a lot we could cover here, but let’s stay focused, we’re building a hello world application, so let’s put a label on here and get started.
- Open up the Toolbox by clicking on it’s anchor on the left hand side of the Visual Studio window.
- Now expand the Common Controls band and left-click, hold, drag, and then drop a Label control onto the design surface.
- Press CTRL+ALT+0 – this will open up the code window for this form.
- Place the following code after the InitializeComponent() call.
this.label1.Text = "Hello World!"; |
So, with that line, your code should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TheQuintessentialHelloWorld_WF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.label1.Text = "Hello World!";
}
}
} |
Press F5, you’ll notice that Visual Studio is doing some things, and when it’s done you’ll get a screen that says Hello World!
Congratulations! You just created the Hello World application in a Windows Forms application!
WPF
So we’ve created the Hello World application in the console, and in Windows Forms, but there is one other available Windows application – WPF – or Windows Presentation Foundation. There are some minor differences between this one and the Windows Forms application, but in the end, we’ll get a window that loads in the OS that says Hello World!