This article, provides you step by step procedure to create program to print hello world in C# console application using Visual Studio and it also explains the code line by line, to make it understandable.
Step 1: Open your Visual Studio, Navigate to "File"-> "New"-> "Project"-> Select "Windows Classic Desktop" from left pane and Select "Console App (.NET Framework)" from right pane -> Name it "Hello World" and click "Ok"
Once the project is created you will a output like below
Step 2: Now, we will write code inside Main
method to print "hello World".
In C#, the command to print anything in text is placed inside "Console.WriteLine("Your Text here")
", so basically it will be "Console.WriteLine("hello World")
", it should be placed inside "Main
" method so complete code will be as below
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
I have removed few lines of code which was starting from "using
", basically those were namespaces which were automatically imported, but we don't need them in this program so to make code more clear, I removed them.
I have a added a line "Console.Readkey()
", it will basically keep our Console Application running, and save it from automatically closing it, until we press any key on keybpard.
Now, let'c click "Ctrl + F5" to compile,build and run project in Visual Studio.
You will see output as below
Let's understand each line of code.
using System
; - This line of code, is basically adding a namespace "System", in this program. Namespace is group of class, to make development easier, we create namespace in C#. System
is the namespace which is provided by .Net Framework.
namespace helloworld
- This project has namespace Helloworld.
class program
- we are declaring class named as "program". As C# language is OOP (Object oriented programming) based, classes are used to define methods/variables etc, where class
is keyword in C#
static void Main(string[] args)
- Let's divide this line code and understand it
static
is a keyword, which is used to define a method that can be used without creating class (Program) object.void
is a return type of method, as this method will not return anything,void
is used.Main()
method is the entry point of our console application.string[] args
defines a parameter which can be used in the method, args is parameter name, while string[] means it is an array of string
Inside braces we have written code to print "Hello World"
Console.WriteLine("Hello World");
Console.ReadKey();
Console.WriteLine("Hello World")
print the "hello World" in console application, while Console
is a class inside System
(namespace used) and WriteLine
is a method inside Console class and "Hello World" is string argument passed to this method
Console.ReadKey()
as another method inside Console
Class of System
namespace, which is used to read input key from keyboard.
Why do we use semicolon at the end of code in line 9-10 of above screenshot?
It is because in C# we're using semicolon to tell the compiler the statement is over.
A closing curly brace means the end of a code block so semicolon is not needed behind curly brace.
You can continue reading next chapter in C# Tutorial which is C# Variables
You may like to read: