Search This Blog

Monday, August 29, 2011

C# 4.0 – Ch:02:E – Projects & Solutions : Continued


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:D – Projects & Solutions

In the previous article, I stopped with AssemblyInfo.cs file under the Properties folder. In this article, I will continue with the References folder.

Check out the image below, where in all the folders are expanded and you can see a list of files under the References folder.

SolutionExplorer1[2]

Under the folder “References”, you will find all the libraries that are referenced automatically for us by Visual Studio. Though not all assemblies that are referenced are necessary for the project we developed, Visual Studio references the most common assemblies that are used while developing a Console based application.

This might be a bit confusing for you right now, since I have already told you that the through Namespaces we tell the compiler which bits of the library we want to use.

If you remember, I had told you that a single library (dll) can contain any number of Namespaces and each Namespace can be split across several libraries (dll’s).

Hence, the C# compiler cannot conclude which libraries the project would need from the Using directives, because in general it’s not possible to presume which libraries are required from the namespaces alone. Hence a project first has to list the libraries it requires and then individual source files in the project have to declare which namespaces they would be using.

Based on the template you select, Visual Studio will automatically refer a set of files that are commonly used, even though you may not use all the references. However, when you compile the project Visual Studio notices the code and omits all the unused references making the binaries smaller than they would be if these unused references were not omitted.

It’s also not necessary that you have to leave it for Visual Studio to omit the unused references. You can add additional references or remove references that you may not require, which is common when developing projects.

To remove a reference, you can just select the library in the Solution Explorer and press the Delete button. And for our demo, our program is so simple that it only requires the mandatory mscorlib library. So you can remove all other references every DLL shown, and the program will still work.

To add a reference to a library, you can right-click on the References folder item and choose the “Add Reference” menu item and from the dialogue window that opens up, select the appropriate library and click OK.

I hope that this is sufficient with regards to the “Hello, C# World!” program. Let us start to explore more of the core language features now. But before moving on, let us recap everything once.

We created a Console Application project using Visual Studio and all we did was to add a single line of executable code in our program that invokes the WriteLine method of the System.Console class to print the message “Hello, C# World!”. We wrote this line inside a method whose special name, Main, marks it out as the method to run when the program starts and this method is contained by a class called Program, since C# requires all methods to belong to a type.

Finally, this class is a member of the Demo1 namespace, because we decide on to follow the convention of having our namespace match the name of the project. Our program uses the using directives supplied by Visual Studio to be able to refer to the Console class without needing to specify its namespace explicitly.

Now if you get back to the code, everything should look very familiar to you now know than it was while writing the code. You should also be comfortable with how Visual Studio behaves.

Also remember that the C# compiler while it is case sensitive does not worry with additional spaces, tabs or even blank lines. The syntax treats any adjoining quantity of whitespace as it would treat a single space.

Also, C# does not care whether you have one statement of code per line, or the code is split across multiple lines or even multiple statements have been written in a single line. All that is required is to mark the end of a statement with a semicolon, so that the compiler knows where the statement ends.

In fact, if you observe the code, you can see that the code is indented to reflect a hierarchy and it uses a lot of spaces or tabs. This is done automatically by Visual Studio to make it easier for the user to read through the code.

Till now, you just saw a single Project in a solution. In the next article I will explain how to go about adding additional Projects into the solution.


…. continued in next article “C# 4.0 – Ch:02:F – Adding Projects to Solution

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