Writing Your First Hello World Console Application in C#


In the following text, we will build our first C# application with, and then without, Visual Studio.Net. We will see how to write, compile, and execute the C# application. Later in the chapter, we will explain the different concepts in the program.



Open Notepad, or any other text editor, and write the following code:



using System;
namespace MyHelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
Console.WriteLine("Hello World,Welcome to http://www.codesforprogrammers.blogspot.com/ ");
}
}
}



Save this with any file name with the extension ".cs". Example: 'MyFirstApplication.cs' To compile this file, go to command prompt and write:



csc MyFirstApplication.cs


This will compile your program and create an .exe file (MyFirstApplication.exe) in the same directory and will report any errors that may occur.
To run your program, type:


MyFirstApplication


This will print Hello World,Welcome to http://www.codesforprogrammers.blogspot.com/ as a result on your console screen. So simple, isn't it? Let's do the same procedure with Visual Studio.Net:



With Visual Studio.Net



Start Microsoft Visual Studio.Net and select File - New - Project; this will show the open file dialog. Select Visual C# Project from Project Type and select Console Application from Template. Write MyHelloWorldApplication in the name text box below and click OK.


Writing Your First Hello World Console Application in C#


Now to compile and execute your application, select Debug - Start Without Debugging or press Ctrl+F5. This will open a new Console Window with Hello World written in it. Once you press any key, the window will close,terminating the program.

Namespaces in C#


A Namespace is simply a logical collection of related classes in C#. We bundle our related classes (like those related with database activity) in some named collection calling it a namespace (e.g., DataActivity). As C# does not allow two classes with the same name to be used in a program, the sole purpose of using namespaces is to prevent name conflicts. This may happen when you have a large number of classes, as is the case in the Framework Class Library (FCL). It is very much possible that our Connection Class in DataActivity conflicts with the Connection Class of InternetActivity. To avoid this, these classes are made part of their respective namespace. So the fully qualified name of these classes will be DataActivity.Connection and InternetActivity.Connection, hence resolving any ambiguity for the compiler.



So, in the second line of our program we are declaring that the following classes (within { } block) are part of
MyHelloWorldApplication namespace.





namespace MyHelloWorldApplication
{
...
}




  • The C# namespaces have NO physical mapping as is the case in Java. Classes with same namespace can be in different folders.

  • The namespace may contain classes, events, exceptions, delegates and even other namespaces called Internal namespace.


These internal namespaces can be defined like this:





namespace Parent
{
namespace Child
{
...
}
}

Template by - Mathew | Mux99