Search This Blog

Wednesday, August 24, 2011

C# 4.0 – Ch:02:C – Hello C# World Up-Close : Continued

If you like this article, please click on +1 button and share with your friends.

Click here for Video Tutorial's.
visit www.iGnani.com

...continued from article "C# 4.0 – Ch:02:B – Hello C# World Up-Close

In our previous article, we created finished with namespaces. Let us continue with our dissection of the code we wrote in the previous article.

The namespace is followed by an open brace ({) and closed with a closing brace (}) and all the code goes in between these two braces. As I have mentioned earlier, C# uses braces to denote a block, where in everything inside these braces that follow the namespace will be a part of the Demo1 namespace.

Namespaces contain types and the code that follows immediately after the opening brace in our code defines a type.

namespace Demo1
{
    class Program
    {
    .....
    }
}


As you can see from the above code, the next part of the code defines a class called Program. If you want to write any code of any kind in C#, it should be within a type definition. In other words, C# code must be contained within a class. The class declaration consists of the class keyword, followed by the class name and a pair of curly braces. All code associated with the class should be placed between these braces.

Now that we have defined the class does not mean that we are ready to write the commands, we are still not quite at it yet. In-fact code lives inside a class, and to be more specific, the code must live inside a particular method inside a class. That is what follows the open brace after the class definition. A method is a named block of code, which may optionally accept some data and optionally return some data.

In our class, we now declare a method called Main(), which once again is followed by a pair of braces to show where it starts and ends:


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

Every C# executable (such as console applications, Windows applications, and Windows services) must have an entry point and in C# the Main() method (note the capital M) is the entry point.

Main method is called when the program is started and if you try to compile an executable without Main, you will receive a compiler error message saying that it can't find the entry point.

Check out the format of method definitions in C#:


[modifiers] return_type MethodName([parameters])
{
    // Method body.
}

In the above format, the first square brackets represent certain optional keywords called Modifiers. Modifiers are used to specify certain features of the method you are defining, such as where the method can be called from. In this case, the static modifier indicates that it’s not necessary to create a Program object (Program being the class that contains this method) in order to use this method. In other words, it indicates that the method does not operate on a specific instance of your class and therefore is called without first instantiating the class.

I will explain modifiers in detail in the coming articles.

Modifier is followed by the return_type, which specifies the return data type from the method. If the method does not return any data and it just does some work it has to be denoted by the keyword “void. On methods that return data, you will use an appropriate datatype based on the data that will be returned.

Since our method is just about displaying a message on the screen, there is nothing for it to return. So we have denoted it by using the keyword “void”.

After return_type, the next part is the MethodName and you can name the method anything as per the rules of C#. In our code, the name given to the method is Main(). Though you can name the method as you like, Main() method has its own importance. Main happens to be a special name. For every executable, the C# compiler expects a program to have at least one static method called Main which will be used at when the program is launched.

The method name is followed by a parameter list, which declares the input, the method requires. However, parameters are optional. I will cover parameters in more detail later on in this series.

In our example, parameter list is “string[] args”, which states that the method expects a single parameter and that the code will refer to it using the name args. The square brackets indicates that the input parameter is a string array (meaning multiple strings in a sequence. will cover arrays later on in the series).

Though we do not use this parameter, but it’s a common practice while defining the Main method. I will cover Main method parameters in more details later on in the series.

Till now all the code that I spoke off was generated by Visual Studio. Let us move on to the final part of the example, which is the code inside the Main method and that is the only part which we included. Take a look at the code below:


static void Main(string[] args)
{
    Console.WriteLine("Hello, C# World!");
}

This shows the C# syntax for invoking a method. Here we are using a method provided by the Console class, which is part of the .NET Framework class library and it is defined in the System namespace. The Console class provides the ability to display text in a console window and to read input typed by the user in an old-fashioned command-line application.

In our code, we invoke the Console class’s WriteLine() method, by passing the text "Hello, C# World!" as the input parameter. The WriteLine method will write the input parameter text we provided on to the console window.

WriteLine() is a static method, so you need not instantiate a Console object before invoking it.

And that completes our dissection of the code which we wrote in our previous article. Now you should have the basic understanding of the code what we wrote, as well as the common statements that results in a simple console application.



C# Tips : Dot (.) in C#

Before completing, I would like to throw some light on the dot (.) symbol. You might have noticed that the dot (.) is being used in different places in different scenarios to mean different things. First we saw it in the using statement

using System.Collections.Generics


where in we used it to break up the namespace, and then we used it with

Console.WriteLine("Hello, C# World!");


to invoke a particular method provided by a class, as shown in the above statement. It just does not end here, it will be used in quite a few other scenarios, which I will explain in the coming articles of this series.


Although covered each and everything from the code in the file Program.cs, that does not mean that we looked at everything. Visual Studio did not just create the template code for us in the file, but it did a lot of work for us. I will cover that in our next article.







…. continued in next article “C# 4.0 – Ch:02:D – Projects & Solutions




visit www.iGnani.com


MicroMind Information Systems
#4013, K.R. Road, Banashankari II Stage,
BANGALORE - 560070
KARNATAKA, INDIA
Phone: +91 80 26762747

Blogger Labels: C# Syntax, Languages, Visual Studio, Visual Basic, Hello World, Program, System, Collections, Generic, Linq, Java, .NET Framework, library, Console, message, output, Organize Usings, Remove Unused Usings, Keyword, Namespace

No comments:

Post a Comment