Search This Blog

Tuesday, August 16, 2011

C# 4.0 – Ch:02:B – Hello C# World Up-close


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

Click here for the Video Tutorial of this article.
visit www.iGnani.com


...continued from article C# 4.0 – Ch:02:A – Language Basics

In our previous article, we created our first C#.NET program, executed it and enjoyed seeing out first program showing the results. But, we never got into the details of what was happening. In this article, let’s look at the code and Dissect it since these are the things you’ll deal with every time you write programs in C#.

Before getting into the code, let’s look at a few general notes about C# syntax. This list does mean everything and just represent few tips. I will continue to cover them as we use them in the coming articles.

  • In C#, as in other C style languages, most statements end in a semicolon ( ; ) and can carry on over multiple lines without needing a continuation character as in other languages such as Visual Basic.
  • Multiple statements can be joined into blocks using curly braces ( {…} ).
  • Single line comments begin with two forward slash characters ( // ), and multiline comments begin with a slash and an asterisk ( /* ) and end with the same combination reversed ( */ ).
  • C# is case sensitive, which means that variables named myVar and myvar are two different variables.

Let us look at the only file that contained code in our previous “Hello C# World!” example, which is the Program.cs, which starts with a few lines starting with the keyword “using”.  

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

The text that follows the using keyword denotes a namespace. The using statement specifies a namespace that the compiler should look at to find any classes that are referenced in the code but that aren’t defined in the current namespace. This is same as the import statement in Java and the using namespace statement in C++.


using directives defines the external code that will be used by the code in this particular source file. In C#, we never come across any code that does not rely on other code. All C#.NET programs depend on the .NET Framework class library. All the namespaces in our example start with System, which indicates that we might be using code from the .NET Framework libraries.

Visual Studio by default adds four namespace directives to the Program.cs file in a new console project. The first one “System” namespace contains general-purpose services, including basic data types such as String, Int etc. More importantly, it also contains the Console type our program uses to display the message “Hello C# World!” and other console-related services, such as reading keyboard input and choosing the color of your output text.

The System.Collections.Generic namespace contains types for working with collections. The System.Linq namespace contains types used for LINQ, which provides convenient ways of processing collections of information in C#. The System.Text namespace contains types useful for working with text.

For our example, we just required the “System” namespace and the next three using directives aren’t used in our example, but Visual Studio adds them to newly created projects since they are most likely to be used in any applications.

Just because Visual Studio included it does not mean that we have to use them. If you feel you will not be using them, you can simply remove them and if you feel you need anything else you can add them as well.



Visual Studio Tips: Removing Unwanted Using Directives

It is not easy to know what directives we are using from the list of using statements generated automatically. But there is a quick way to remove unwanted using directives.

Right-Click anywhere on your C# code and from the context menu select “Organize Usings” and from the submenu select “Remove Unused Usings” item. This will remove the unused using directives from the code. The submenu offers two more options aimed at those who like to keep their source code neat. These options speak for themselves and just do what they claim. You can see the menu from the image below.



OrganizeUsings


Namespaces does not stop here in our program. In fact, the very next line of code after these directives is also related to namespaces:


namespace Demo1
{
    .......
}

While using directives declare which namespaces our code consumes, this namespace keyword tells the compiler what namespace we are developing. All the types we write in our programs belong to namespaces just like the types in the class library.

Here, Visual Studio has assumed that we would like to put our code into a namespace named after the project we created (Demo1), since this being a common practice. However, this does not mean that you have to stick to it. You are free to call your namespace whatever you feel appropriate. The namespace keyword declares the namespace your class should be associated with and all code within the braces that follow it is regarded as being within that namespace.

I will cover Namespaces in detail in a separate article later on. For now, let us continue with our dissection of the code we wrote in the previous article.







…. continued in next article “C# 4.0 – Ch:02:C – Hello C# World Continued




visit www.iGnani.com


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

No comments:

Post a Comment