How to Write a “Hello World” App in C#

Hi fellow developers! In this post, we will see step by step, how to write a “Hello World” app in C#.

 

Introduction

Visual Studio is an extremely powerful IDE for building many types of applications.

In this dev tip, we are going to build a “Hello World” Windows forms application in C#.

If you don’t already have Visual Studio, the you can download Visual Studio Community Edition which is free.

Ok, let’s build our Hello World app!

In this example I’m using Visual Studio 2013 and .NET Framework 4.5 but the philosophy is the same for newer versions of Visual Studio and .NET Framework.

 

Developing the “Hello World” App in Visual Studio

So, after launching Visual Studio, we click on “File“, “New“, “Project“.

How to Write a "Hello World" App in C# - Article on {essentialDevTips.com}

 

We then select the template for this new project, in this case “Windows Desktop” (1), the version of .NET Framework to be used (2), the type of the project, in this case “Windows Forms Application” (3), the name and disk location of the project (4 and 5), and finally the solution name (6) which by default gets the same name as the project name but of course you can change it if you want. After all these are set, we click on the “OK” button.

How to Write a "Hello World" App in C# - Article on {essentialDevTips.com}

 

Right after we click OK, the new project is ready for development. The below screenshot illustrates the development environment for this Windows Forms project.

How to Write a "Hello World" App in C# - Article on {essentialDevTips.com}

 

The next step is to drag and drop a “button” control from the toolbox right onto our form (named Form1 – you can change the name in the Properties dialog on the lower right section of the IDE).

How to Write a "Hello World" App in C# - Article on {essentialDevTips.com}

 

Right after we add the button, we set the two below properties in the Properties dialog which can be found on the lower right section of the IDE):

  • Name: btnClickMe
  • Text: Click Me!

Now we need to set the button’s behavior when clicked. We double-click on the button control on the form and an “event handler” for the mouse click event on the button is automatically created. Now all is left is to add to the event handler the code we want to be executed when the button is clicked. To this end, we add the code: MessageBox.Show("Hello World! This is a C# program!");

How to Write a "Hello World" App in C# - Article on {essentialDevTips.com}

 

Right after adding the code, we press F5 or click on the Start button (has the “play” sign) and if everything is OK, our program runs.

How to Write a "Hello World" App in C# - Article on {essentialDevTips.com}

 

So let’s test our program by clicking on the “Click Me!” button:

How to Write a "Hello World" App in C# - Article on {essentialDevTips.com}

That’s it! As you can see, when we click the button a popup (message box) is presented displaying the “Hello World” message!

 


Recommended course: “.NET Programming for Beginners – Windows Forms with C#”

In this course for beginners, you will get started with .NET Windows Forms (WinForms) Programming using Visual Studio and the C# Programming Language. You will learn the basics of .NET Windows Forms, event handling, multithreading and how you can create deployment packages for your .NET Windows Forms programs, directly via Visual Studio, using the ClickOnce component.

Last but not least, throughout the course, we will be developing in different phases, a Text Editor demo app, using Visual Studio, C# and Windows Forms.

.NET Programming for Beginners - Windows Forms (C#) - Enroll to the Course
(Lifetime access, downloadable resources, course completion certificate and more!)

Enroll to the Course


 

Recommended Online Courses:

 

Read Also:

 

Reference: {essentialDevTips.com} (https://www.essentialdevtips.com/)

© essentialDevTips.com

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)

Loading...