21 Mar, 2009 · 4 minutes read
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.
First Step:Create a New Console Application in Visual C#
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. A namespaceis an abstract collection of classes. The .NET Framework has a huge set of classes. They are grouped under namespaces for easy accessibility, better categorization and to prevent name clashing. For Example, the System.IOnamespace 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 inconveniences too. Take the System.Console.WriteLine()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 usingkeyword comes into aid by importing the namespaces. When imported, the classes inside the namespace can be accessed just by their names. So, once we have imported the Systemnamespace, the above method can be called as Console.WriteLine().
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 Mainfunction. This is the entry point for the application. Think of it as the outer door of your house. Any one coming 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 our application. This is why we use the string[] argsinside 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 Consoleclass resides in the System namespace and represents the standard input, output and error streams for console application. The WriteLinemethod 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: Console.ReadLine();
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();
}
}
}
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 cannot be used directly. To display the special characters such as these, we use escape sequences. All escape sequences begin with the backslash character \
Example Usage:-
Console.WriteLine("This is a \n multi-line text");
Output:-
This is a
multi-line text
Console.WriteLine("C# is a \"Programming Language\"");
Output:-
C# is a “Programming Language”
Here’s the list of the escape characters in C#
Escape Sequence | Equivalent Character |
---|---|
\’ | Single Quotation |
\” | Double Quotation |
\\ | Backslash |
\0 | NULL |
\a | Alert |
\b | Backspace |
\f | Form Feed |
\n | New Line |
\r | Carriage Return |
\t | Horizontal Tab |
\v | Vertical Tab |
Note: Equivalent for Visual Basic’s vbCrLf is \r\n