Your First Console Application in C#
- Introduction to Programming
- What is C#?
- Your First Console Application in C#
- Introducing Methods and the Main() Function in C#
- Introducing C# Classes and Structs
- C# Data Types, Variables and Casting
- C# Program Flow Control and Entry Points
- Passing Parameters to Methods and Return Values in C#
- C# Access Modifiers and Scope
- C# Interfaces and Classes
- Using Namespaces in C#
- C# Conditional Statements
- Looping and Iteration in C#
- Using Arrays and Lists in C#
- C# Constants and Read-Only Variables
- Error and Exception Handling in C#
- Using Recursion in C#
- C# Operator List
- Class Inheritance in C#
- C# Class and Method Attributes Explained
- C# Class Constructors and Destructors
- C# Generics Variables
- XML Serialization and Deserialization
- 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.
- using System;
- class HelloWorld
- static void Main()
- Console.WriteLine("Hello World!");
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