IMPORTANT PLEASE READ!
Website development and design blog, tutorials and inspiration

Your First Console Application in C#

First Console Application, Hellow World!

By , Written on in C#

Your First Console Application in C#

419 words, estimated reading time 2 minutes.

In this short tutorial, we will create a simple C# Console Application to verify that our version of Visual Studio has been installed correctly and that the .Net Framework is working properly. We will create the infamous "Hello World" application while demonstrating some important concepts of C# programming.
 
C# Programming Series
  1. Introduction to Programming
  2. What is C#?
  3. Your First Console Application in C#
  4. Introducing Methods and the Main() Function in C#
  5. Introducing C# Classes and Structs
  6. C# Data Types, Variables and Casting
  7. C# Program Flow Control and Entry Points
  8. Passing Parameters to Methods and Return Values in C#
  9. C# Access Modifiers and Scope
  10. C# Interfaces and Classes
  11. Using Namespaces in C#
  12. C# Conditional Statements
  13. Looping and Iteration in C#
  14. Using Arrays and Lists in C#
  15. C# Constants and Read-Only Variables
  16. Error and Exception Handling in C#
  17. Using Recursion in C#
  18. C# Operator List
  19. Class Inheritance in C#
  20. C# Class and Method Attributes Explained
  21. C# Class Constructors and Destructors
  22. C# Generics Variables
  23. XML Serialization and Deserialization
  24. C# String Formatting Examples

Having just installed Visual Studio, we should just test that the installation is working ok. To do this we will create a "Hello World" console application. We will introduce a few constructs that may be unfamiliar, but don't worry, we will cover these in greater detail in the upcoming tutorials.

For now, open up the Visual C# and create a new Console Application. This can be done from the File menu and select New Project, then Console Application.

In order to keep things as simple as possible for the first lesson, select all the code (Ctrl+A) or Edit » Select All, and delete it. Next, you need to type in, or copy and paste the following code into the editor.

  1. using System;
  2.  
  3. class HelloWorld
  4. {
  5. static void Main()
  6. {
  7. Console.WriteLine("Hello World!");
  8. }
  9. }

You should now run the program by pressing (Ctrl+F5) which will start without debugging, or from the menu select Debug » Build Without Debugging. I'll explain why in the next tutorial, but now you should see the console window that says "Hello World!"

Let's Examine the Code

On the first line, we are telling the compiler that we want to use something called System. The system is a namespace that is provided by Microsoft. Namespaces are an important concept in C# (and the .Net Framework in general) and will be covered in a future tutorial. It is similar to include statements in languages such as C/C++ and PHP.

The next line of code is creating a class called HelloWorld. Again, a class is a fundamental concept in object orientated programming and will be covered in great detail in future tutorials. For now, think of a class as a container. Everything must be contained within a class in C#, unlike C/C++ and PHP where you can have variables inside or outside a class or function.

The next line of code defines the Main function for the program. We don't need to worry too much about Main at this stage, as we will cover it in a later session. Finally, our last line of code simply tells the program to write a line onto the screen. Console.WriteLine will write a line of text to the console screen and automatically add a newline, carriage return to the end. Notice there is a semi-colon at the end of the line. C#, like a lot of programming languages, uses the semi-colon to separate lines of code or operations. Every statement will have a semi-colon at the end.

Last updated on: Saturday 24th June 2017

Did you Like this Post? Why not Like us on Facebook?

 

Comments

There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.