Jump to content
xisto Community

C# Tutorial : Lesson 1 - Hello World Program

Recommended Posts

This is the first installment of my venture to explain the bits and pieces of C# that I have managed to learn at NIIT. In this lesson we would be creating the Hello World application.


STEP 1: Create a New Console Application in Visual C#


Posted Image


The IDE automatically adds the following code:-


using System;using System.Collections.Generic;using System.Text; namespace HelloWorld{	class Program	{		static void Main(string[] args)		{		}	}}
Lets take a look at the first 3 lines. All of these lines begin with the keyword using. The .NET Framework has a huge set of classes. They are grouped under namespaces for easy accessibility and better categorization. For Example, the System.IO namespace contains all the classes, functions & methods required for Input Output operations, thus making life easier for us human beings. While the namespaces have remarkable benefits, they tend to bring in some potential inconveniances too. Take the System.Convert.ToString() method for example. The method has to be called along with its complete path in the hierarchy, a really tiresome process to do repeatedly. This is when the using keyword comes into aid by importing the namespaces. When imported, the classes inside the namespace can be accessed just by their names.


Any .NET application that we make, has to reside in a namespace of its own, so we write the statement namespace HelloWorld. All the classes you create under this namespace can be accessed as NameSpaceName.ClassName.


Next we have the class declaration itself. Inside you'll find the static void Main function. This is the entry point for the application. Think of it as the outer door of your house. Any one comming inside has to first pass through it. The entry point must be declared as static because that way the main function can be invoked even without creating an object of the Program class. Also the name of the entry function should be Main, the capital M counts too.


Any application can take parameters, when run from the command prompt. Say you ran the Notepad application as follows Notepad.exe MyFile.txt. The text MyFile.txt is the parameter passed to it, Notepad reads this parameter and opens the file. Similarly, we may need to work with arguments passed to your application. This is why we use the string[] args inside the Main function.


Up until this point I have described the auto generated code. Now let us type in some code of our own. Keeping with the tradition of the Hello World program we type in Console.WriteLine("Hello World"); inside the body of the Main function. The Console class resides in the System namespace and represents the standard input, output and error streams for console application. The WriteLine method displays the supplied argument(s) on the console.


Press F5 to run the application. You'll see a console window with the text Hello World appear. Don't worry if the window closes on its own. Its because the application reaches the end point and exits. You can add the following line to make it halt for an input before exiting:



This is how our final code looks like:-


using System;using System.Collections.Generic;using System.Text; namespace HelloWorld{	class Program	{		static void Main(string[] args)		{			Console.WriteLine("Hello World");			Console.ReadLine();		}	}}
Escape Sequences


Much like C/C , C# has a list of escape sequences. Escape Sequences or Escape Characters are some special characters which cannot be displayed directly. For Example, the newline character or the Double Quotes.


White space being ignored by C#, the newline escape sequence is the only way to display the newline character in the output. The double quotes are used to enclose strings hence they can’t be used directly. To display the special charcters such as these, we use escape sequences. All escape sequences begin with the backslash character \


Example Usage:-


Console.WriteLine(”This is a \n multiline text”);



This is a

multiline text


Console.WriteLine(”C# is a \”Programming Language\”");



C# is a “Programming Language”


Here’s the list of the escape characters in C#


Posted Image


Note: Equivalent for Visual Basic’s vbCrLf is \n\r


Next Tutorial - Lesson 2 - Variables & Operators



Lesson: 1 2 3 4 5 6 7


Edited by turbopowerdmaxsteel (see edit history)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.