C# Hello World Program Step by Step using example

Introduction

Hello Friends, very warm welcome to Questpond's new venture site, since we got lot of suggestions from our customers that there are so many topics under microsoft technology and Questpond is involved in every topic with its videos and tutorial article and to read or study every topic under one roof it was bit stressful so they have suggested to make a separate venture site dedicated to particular topics. So we accepted their feedback and created separate websites like we have for MSBI (learnmsbitutorial), Sharepoint and now this is only for CSharp.

About Author & Questpond : I'm Gurunatha Dogi a software developer having more than 9 yrs of exp in Software and Programming technology, enjoys doing open source coding and to create bootstrap websites. Apart of this i'm a trainer @ #Questpond and do take trainings on ASP.NET, Microsoft Business Intelligence, C# ASP.NET, PHP/MYSQL, SQL SERVER, HTML5. So you want to learn or take training with me feel free to touch base with me. Questpond is an educational firm and e-learning firm having many educational books, videos and corporate trainings. Our motto to spread education and we are serving this successfully since 2002.

Now coming back to this first article here we will help you to understand to write your first "HELLO WORLD" program in a visual studio 2015. So let's start it.

This article is purely for freshers or who want to start their c# coding this article will help you to get started with each thing to know to write a "HELLO WORLD" program.

How to download and install Visual studio 2015 we will cover this topic in our second article since it was first article so wanted to keep it simple and short.

Step 1

Open visual studio 2015 and open it, once it is opened Go to File -> Project as shown in below image.

Click on project and create a new Visual C# Console Application, so for that select Csharp as language and console application as a project give a nice name and proceed further as shown in below image.

Finally click on OK button to create a new console application project.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}


Step 2

There you can see our project is loaded successfully with some source code. Let me explain it one by one.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

These are system class libraries which are requied to to execute a project. For example "using System" is a namespace which contains base classes that defines value and reference types, interfaces, exeception handlers and so on.

So to ensure a program runs without any system errors make sure you have that above following namespaces.

Since we are taking too much about keyword "namespace" let's understand it, "Namespace" is a keyword "namespace" followed by NamespaceName in c# is a set symbols that are used to organize classes and objects of various kinds .Namespace is used in two ways. .NET uses namespace to organize many classes for example using System is a namespace inside this namespace you will be able to access Console class same way if you want to access Class FileStream or File then you must need to reference namespace System.IO it allows reading and writing to files and data streams. Second way where we declare our own custom namespace to organize set of classes this helps in maintainability in a large projects. At the same time it is also helps to do reference in another scope of project namespace where use of namespace to provide access classes declared inside that namespace.

Hope you have understood use of namespace and why we declare those namespaces to write a simple console prgram.

Step 3

    class Program
    {
        public static void Main(string[] args)
        {
        }
    }

class : Class is a template or blue print. It defines the data and behavior of a type, inside a class we can define variables, methods, objects, events and our own custom types.

public is the visibility : Public is an access modifier, there are other available access modifier in C# like private, protected, internal and protected internal. All these modifiers have their own role and their access visibility to methods. Here public means method visibility is everywhere.

Static : static is a special [optional] keyword : Static methods can be called without creating an instance of a class or without object of a class, we can access it directly.

void : It is the return type of a method and void does not return anything. So when you define a method then it must have return type.

main() : It is name of a method, since methods must have to be named and main() method is also start of a program. In a console application main() methods executes first like a startup.

string[] args : String is a datatype and here string is a array with a array name 'args' that contains the supplied command-line arguments as an array of String objects

Step 4

Here in this step let's print out "Hello World" and in a Csharp console application to print any thing we need to System class Console and using console we need to call WriteLine, WriteLine is a method which helps to print anything in a program. Code demonstration is shown below.

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
        }
    }
}

Here is the program output.

Step 5

Here in this step let's understand how our solution explorer is structured for this console application.

Solution Explorer provides us with an organized view of your projects from here you can access any file directly it is like detailed view of your project files with their supporting files.

At the top we have solution name or project name "ConsoleApplication2".

Properties tab : Inside that we have "AssemblyInfo.cs", This file contains information about this console application like AssemblyTitle, AssemblyCopyright, version (.NET Framework version), description, name, ecc.

References : It will provide detailed information about namespace or project namespace which have been referenced here. For example "System" it is a namespace referenced and same accessed in a coding "using System". If anytime you need to access any namespace first it should be referenced here.

App.config : It is a simple XML file where project settings can be configured like for example we can define settings of sql connection string .

<?xml version="1.0"?>
<configuration>
    <connectionStrings>   
        <add name="MyQuestpondSqlKey" 
             connectionString="Data Source=localhost;Initial Catalog=ABC;"
             providerName="System.Data.SqlClient"/>
    </connectionStrings>
</configuration>

Program.cs : It is name of a Csharp file where we are doing actual coding.

So hey friends this about it, Hope you enjoyed reading this article. Kindly do not forgot to share this article on you social channels like facebook, Google+, Twitter and so on...Thank you..!!!

We will come soon with more and more tutorial article and videos in C#. We have covered one nice video i.e. Learn C# in 100 hours. It covers everything and helps you to get going with C#. Video is available in our Videos section.

+91-22-66752917
+91 9967590707
questpond@questpond.com / questpond@yahoo.com