c# 4

42
C# is an international standard programming language used to create instructions that direct the computer about what to do, when to do it, and how to do something. C# 4 is the new version of the C# computer language. This web site is dedicated to C#, providing lessons, tutorials, and topics on various issues. To follow the lessons on this site, you must use at least the csc compiler, which you get from having the .NET Framework installed in your computer. The best way is to use either Microsoft Visual C# 2010 Express, which is free, or Microsoft Visual Studio 2010. C#, pronounced c sharp, is a programming language that can be used to give instructions to a computer. The instructions can be written from a text editor such as Notepad. Another way is to use a programming environment that is equipped with many tools that make it easy to work on projects, to create the necessary files, and to distribute a completed application. Many programming environments are available. Probably the most popular programming environment you can use to write C# instructions is Microsoft Visual Studio. This is commercial application available for purchase from Microsoft. If your primary goal is to learn the C# language, Microsoft provides a free programming studio you can use: Microsoft Visual C# 2010 Express. To follow our lessons, you can use Microsoft Visual Studio 2010 or Microsoft Visual C# 2010 Express. To get Microsoft Visual C# 2010 Express, get to the Microsoft web site and click the Visual studio link.

Upload: pankaj-bhargav

Post on 22-Nov-2014

42 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C# 4

C# is an international standard programming language used to create instructions that direct the computer about what to do, when to do it, and how to do something.

C# 4 is the new version of the C# computer language.

This web site is dedicated to C#, providing lessons, tutorials, and topics on various issues.

To follow the lessons on this site, you must use at least the csc compiler, which you get from having the .NET Framework installed in your computer. The best way is to use either Microsoft Visual C# 2010 Express, which is free, or Microsoft Visual Studio 2010.

C#, pronounced c sharp, is a programming language that can be used to give instructions to a computer. The instructions can be written from a text editor such as Notepad. Another way is to use a programming environment that is equipped with many tools that make it easy to work on projects, to create the necessary files, and to distribute a completed application.

Many programming environments are available. Probably the most popular programming environment you can use to write C# instructions is Microsoft Visual Studio. This is commercial application available for purchase from Microsoft. If your primary goal is to learn the C# language, Microsoft provides a free programming studio you can use: Microsoft Visual C# 2010 Express.

To follow our lessons, you can use Microsoft Visual Studio 2010 or Microsoft Visual C# 2010 Express. To get Microsoft Visual C# 2010 Express, get to the Microsoft web site and click the Visual studio link.

Starting Microsoft Visual C# Express

Page 2: C# 4

To launch Microsoft Visual C# 2010 Express, you can click Start -> (All) Programs -> Microsoft Visual Studio 2010 Express -> Microsoft Visual C# 2010 Express:

To launch Microsoft Visual Studio 2010, you can click Start -> (All) Programs -> Microsoft Visual Studio 2010.

The Microsoft Visual Studio Interface 

The Main MenuThe top section of Microsoft Visual Studio displays the main menu divided in categories such as File or Edit. The main menu is used like the menu of a normal application.

The ToolbarsUnder the main menu, there is a toolbar. Normally, when the studio is launched, it displays the Standard toolbar as its default. Some additional toolbars will display in response to an action from you. Still, you can display other toolbars if you want. To do this:

On the main menu, click View -> Toolbars and click the toolbar of your choice

Right-click the main menu or any toolbar. In the that appears, click the desired toolbar

Page 3: C# 4

Toolbars are used the same way they are in other applications.

Microsoft Visual Studio's menus and toolbars can be customized. You can customize a menu category on the main menu by adding a menu item to it. You can customize a toolbar by adding a button to it. To start, right-click anything on the main menu or on any toolbar and click Customize... For example, imagine you want to add an item named Start Without Debugging to the left side of the Start button:

To do this:

a. Right-click the main menu or a toolbar and click Customize

b. On the Customize dialog box, click the Commands tab

c. Click the Toolbars radio button

d. In the Toolbars combo box, select the toolbar that will host the button. For our example, that would be Standard

e. In the Controls list, click the button that will come after the new button. For our example, that would be Start / Continue:

Page 4: C# 4

f. Click Add Command...

g.  In the Categories list of the Add Command dialog box, select the category that has the button you want. For our example, that would be Debug

h.  In the Commands list, click the desired button or menu item. For our example, that would be Start Without Debugging

Page 5: C# 4

i. After making the selection, click OK

j. On the Customize dialog box, click Close:

Page 6: C# 4

In the same way, you can customize any toolbar and any menu category.

 

The Start PageThe Start Page is the main body of Microsoft Visual Studio or Microsoft Visual C# Express when it has been launched. It starts on top with a tab labeled Start Page. The main body of Microsoft Visual C# Express displays Microsoft Visual C# 2010 Express. The left section shows two labels: New Project... and Open Project... When some projects have been previously created or used, their names appear under Open Project... When you work on a project, the Start Page is usually in the background but it is represented by a tab. At any time, to display the Start Page:

You can click the Start Page label in the top section of the Code Editor

On the main menu, you can click View -> Start Page

Showing and Closing a WindowWhen you start or open a project, the studio makes some windows available. These are the most regularly used windows. If you think that one of them is not regularly used in your types of assignments, you can remove it from the screen. To hide a window, you can click its Close button  .

All of the windows you can use are listed in the View menu. Therefore, if a window is not displaying, you can click View on the main menu and click a window of your choice.

Auto Hiding a WindowWhen working on a project, you will use a set of windows that each accomplishes a specific purpose. Some windows are represented by an icon but hide the rest of the body. To display such a window, you can position the mouse on it. This would expand the window:

Page 7: C# 4

If you expand a window, it would display a title bar with three buttons. One of the button is called Auto Hide:

If you expand a window but find out you don't need it any more, you can just move the mouse away. The window would return to its previous state. Based on this functionality, if you are working with a window and move the mouse away, it would retract. If you need it again, you would have to reopen it using the same technique. If you are going to work with a certain window for a while, you can keep it open even if you move the mouse away. To do this, you can click the Auto Hide button. If clicked, the Auto Hide button changes from pointing left to pointing down:

Page 8: C# 4

By default, the studio installs some windows to the left and some others to the right of the screen. You can change this arrangement if you want. To do this, expand a window, then click its title bar and start dragging. While you are dragging, to assist you with a new position, the studio displays five boxes:

Docking a Window to a Side of the StudioTo position a window on one side of the studio, drag its title bar to the box that represents that side. When you get to a side, a transparent blue rectangle would be drawn to indicate the possible position. Drop the window to either the most left guiding box to position it to the left of the screen, or to the most right guiding box to position it to the right of the screen. Here is an example:

Page 9: C# 4

or

Page 10: C# 4

To position a window to the top or the bottom side of the studio, drag its title bar to either the most top guiding box to position it to the top of the studio or to the most bottom guiding box to position it to the bottom of the studio. Here is an example:

Docking a Window Above or Below AnotherTo make two windows share an area, you can position one above or below an existing window. Of course, you must first have a window as reference. Then, drag the second window to the existing window. Drop it in the top or the bottom guiding box of the middle big box.

This

Page 11: C# 4

Becomes

Page 12: C# 4

Floating WindowsMost of the windows you will use are positioned on one side of the screen. If you want, you can have a window that stays on top of other windows but is not "glued" to one side. Such a window is said to float. To float a window, drag its title bar and release it somewhere in the middle of the screen but not on a guiding box.

When a window is floating, it displays a Maximize button   on the title bar. If you click this button, the window occupies the whole interface of Microsoft Visual Studio. In the same way, if you double-click the title bar, the window gets maximized.

If a window is currently floating and you want to dock it, right-click its title bar and click Dock:

Page 13: C# 4

If a window is currently docked and you want to float it, right-click its title bar and click Float.

Coupling WindowsYou can make two or more windows share one side of the screen or to share an area. To do this, first expand and dock the window that you will use as the base (or reference). Then, drag the title bar of the window that will share the area to its target until the bottom part of the base window shows a highlighted and a non highlighted area:

Page 14: C# 4

Then release the mouse:

Page 15: C# 4

To remove a window that shares an area, drag its tab away:

To position a window to the left or the right of an existing window, first drag the second window to where the existing window is. Then click the left (or the right, depending on the side you want) guiding box of the multiple-box.

This

Page 16: C# 4

becomes this

Page 17: C# 4

 In the same way, you can make two or more windows share the same area.

Tabbed WindowsYou can make a window display its tab in the top section of the Code Editor and share the area with it. There are two ways yon do it:

You can drag a window and drop it on the tab of the Start Page.

This

becomes this

Page 18: C# 4

If a window is already represented with a tab on top of the Code Editor, you can drag the second window and drop it to the left or of the right tab of the existing window.

You can right-click Dock as Tabbed Document

Introduction to C# Projects   

A C# Project 

Introduction to C# as a Computer LanguageA computer programming language, also called a computer language, or a programming language, is used to give instrauctions to a computer to perform a specific assignment. Like a spoken language, a computer language has meaning words, syntaxes, grammar, formulas, and rules that must be followed to construct meaningful sentences.

Just like there are many languages, there are also various types of computer languages. A significant characteristic of computer languages, as opposed to human languages, is that they are categorized by roles or purposes.

Hardware is a series of objects that can receive instructions from a computer language. The hardware can consist of all the parts used on a cell phone, a microwave oven, a car, the roller coaster of an amusement park, a health care machine, or a personal computer. Some languages deal with specific hardware or some instructions can target a specific environment, such as a cell phone or a roller coaster. Such an environment uses a specific operating system that cannot be carried from one machine to another. Some other languages do not specify the hardware they are meant to serve.

Page 19: C# 4

Some languages are used to solve a specific type of problem and they cannot perform general assignments. For example, some languages used on the Internet are meant to display things on a browser. Such languages cannot perform arithmetic or geometric calculations. Some other languages are used to operate on specific values, such as true orother languages are used to solve all, or almost all, types of problems. Languages in this latter category are probably the most regularly used. These are refferred to as general purpose languages.

C#, pronounced c sharp, is a general purpose programming language used to create instructions for the computer to solve a problem or to assist a user with performing an assignment. C# is a universal language that is used on many operating systems, including Microsoft Windows. C# is one of the languages used in the Microsoft .NET Framework. The Microsoft .NET Framework is a library of objects that create or draw things on the computer.

Introduction to C# ProjectsA computer project is a group of files and objects that contain the necessary instructions for the intended application to produced the desired results. A C# project is made of one or more files that contain the instructions of the project. A simple C# project can contain one file that holds all the necessairy instructions. An advanced project would contain more than that. To narrow our goal, our lessons will focus only on the C# language. The language is very huge so much that we may need more than fourty lessons just to cover the essentials. But don't be discouraged. This language is fun and helps you understand many things about computer programming. Also, we assume you don't know any language. So we will learn scratch.

Introduction

Console ApplicationsThe C# language is used to create applications that display on a black window referred to as the DOS prompt or DOS window. Those are the types of applications we will create in our lessons. To start such an application, you can write the instructions in a normal text editor such as Notepad or you can use a programming environment such as Microsoft Visual Studio or Microsoft Visual C# 2010 Express. Our lessons assume that you have access to one of those. We also assume that you have installed the programming environment you will use to follow the lessons, unless you decide to use Notepad, which is part of Microsoft Windows.

To launch Microsoft Visual C# 2010 Express, you can click Start -> (All) Programs -> Microsoft Visual Studio 2010 Express -> Microsoft Visual C# 2010 Express:

Page 20: C# 4

To launch Microsoft Visual Studio 2010, you can click Start -> (All) Programs -> Microsoft Visual Studio 2010.

Application: Starting a Project1. Start Microsoft Visual C# 2010 Express or Microsoft Visual Studio 2010

2. To create a new application, on the main menu, click File -> New Project...

3. In the middle list, click Console Application

4. Change the Name to gdcs1 (which stands for Georgetown Dry Cleaning Services 1)

Page 21: C# 4

5. Click OK

Creating a File for CodeThe programs we will write are meant to give instructions to the computer. You write these instructions in easy to understand English words. We will find out what those words are. This means that a regular instruction uses normal text with alphabetic characters, numbers, and non-readable symbols.

You can write your instructions using any text editor such as Notepad, WordPad, WordPerfect, or Microsoft Word, etc. When writing your instructions, there are rules your must follow and suggestions you should observe. The group of instructions used by your program is also referred to as code.

To assist you with writing code, if you use Microsoft Visual C# Express or Microsoft Visual Studio Professional, it includes a text editor referred to as the Code Editor. If you create your project as a Console Application, a default file would be created and you can customize it. If you start your program as an empty project, you must explicitly add a file to it. To do that:

Page 22: C# 4

On the main menu, click Project -> Add New Item...

In the Solution Explorer, right-click the name of the project, position the mouse on Add, and click New Item...

Any of these actions would display the Add New Item dialog box. From there, in the middle list, click Code File. Accept the name or change it. When you click OK, a blank document would display. In the same way, you can add as many files as you need for your project.

Application: Creating a File1. To add a file for the code, on the main menu, click Project -> Add New Item...

2. If you are using Microsoft Visual Studio, in the left list, expand Visual C# Items and click Code.In the middle list, click Code File

3. Change the name to CleaningOrder

4. Click Add

Page 23: C# 4

The Primary C# CodeTo create the most basic application in C#, there is a very basic code you must write. This code looks as follows:

class Exercise{ static void Main() { System.Console.WriteLine("Hello world"); }}

This code contains many things, in fact all of its things, we cannot explain at this time. We rather wait for appropriate sections in later lessons. Unfortunately, each one of the items in this code is required:

The class keyword will be introduced in Lesson 5

The word Exercise is just a name. We will review the rules of names in the next lesson

 The curly brackets, { and }, and the parentheses, ( and ), will be introduced in Lesson 4 but various ways of using them will be explained in many other lessons

The use of the static keyword will be explained in Lesson 10

The meaning of the void keyword will be reviewed in Lesson 5

Main will be introduced in Lesson 7 and expanded in Lesson 26

The role of the word System is explained in Lesson 8

The role of the period "." will be introduced in Lesson 5

Console and WriteLine will be introduced in Lesson 6 and expanded in other lessons

The double-quotes " will be formally introduced in Lesson 4 and heavily used in Lesson 28

We kept this code to a minimum. Every word and every operator in this code is required but there are various ways that each of these words and operators can be used. For now, please use this code "as is". We can safely promise that by the end of these lessons, you will know everything about those words.

Application: Writing Primary Code In the empty document, write the following code

class Order { static void Main() { System.Console.WriteLine("Georgetown Dry Cleaning Services"); System.Console.ReadKey(); }

}

CommentsA comment is a line or paragraph of text that will not be considered as part of your code. There are two types of comments recognized by C#.

To display a comment on a line of text, start the line with two forward slashes //. Anything on the right side of // would be ignored. Here is an example:

// This line will be ignored. I can write in it anything I want

The above type of comment is used on only one line. You can also start a comment with /*. This type of comment ends with */. Anything between this combination of /* and */ would not be read. Therefore, you can use this technique to span a

Page 24: C# 4

comment on more than one line.

While you can manually create comments, Microsoft Visual Studio provides a tool that can assist you. To add a comment

on a line, click anything on that line. Then, on the Standard toolbar, click the Comment Out the Selected Lines buttonTo add comments to many adjacent lines, select text on those lines and click the Comment Out the Selected Lines

button  . To remove the comments, click the line or select text on the lines. Then, on the Standard toolbar, click the

Uncomment the Selected Lines button  .

Application: Creating Comments To create comments, change the file as follows:

// Application Name: Georgetown Dry Cleaning Services class Order { // This application is used to perform processing orders for this business static void Main() { /* Always greet the customers before processing an order. Check the types of clothes a customer has brought. Find out the type of cleaning (starch, etc) the customer wants. If there are special needs the customer has, make a note. */ System.Console.WriteLine("Georgetown Dry Cleaning Services"); System.Console.ReadKey(); }

}

Managing Files

IntroductionIn Microsoft Visual C# 2010 Express and in Microsoft Visual Studio, when a project is made of various files, each file is represented by a label in the top section of the Code Editor. Here are examples:

Each file is also represented in the main menu under Windows.

Page 25: C# 4

The Solution ExplorerThe Solution Explorer is a window that displays a list of the files that make up a project. To access the Solution Explorer:

If the Solution Explorer is not yet showing on the screen:

o On the main menu, click View -> Solution Explorer

o On the Standard toolbar, click the Solution Explorer button 

If the Solution Explorer is already showing, click its tab

The Solution Explorer is made of four sections. Like every regular window, the Solution Explorer is equipped with a title bar that displays its name on the left side and three buttons on the right side:

The Window Position button   displays a menu when you click it: 

 The Float option is enabled if the window is docked to a side of the screen. The Float option is disabled if the window is already floating. If the window is docked and you click Float, it would be moved from its docked position and would float. As an alternative to float a docked window, drag its title bar away from its docked position: 

To dock a floating  window, drag its title bar

Under its title bar, the second section of the Solution Explorer is a toolbar:

The Properties button allows you to display the Properties window

The Show All Files button is used to show the hidden files of the project

As its name indicates, the Refresh button is used to refresh the list of files and resources of the project

The View Code button is used to show the code of a class

The third part of the Solution Explorer is its body. It shows the folders, files, and resources that are part of the current project. To expand a node, you can either click its button or double-click its name. To collapse a node, either click its button or double-click it.

The root of the list is the name of the solution. Under the root is the name of the current project. If the solution contains

Page 26: C# 4

more than one project, the name of each project is represented under the solution. Inside of the project are its folders, files, and resources. The first item under a project name is References. After the References node, there are the names of the classes that are part of the project.

The fourth part of the Solution Explorer is its tab.

The Properties WindowThe Properties window shows the Windows operating system's details of the files or resources used in a project. To display it:

If the Properties window is not yet displaying

o On the main menu, click View -> Properties window

o On the Standard toolbar, click the Properties button 

If the Properties window is displaying already, click its tab

The display and rectangular behavior of the Properties window follows the description we had for the Solution Explorer.

To show the operating system's characteristics of a project or a file, in the Solution Explorer, click the object:

If you click a solution, the Properties window would show its name and its location

If you click a project, the Properties window would show its project file

If you click a file, the Properties window would show its name (in the File Name field) and its location (in the Full Path) field

The Properties window displays different fields depending on the item selected in the Solution Explorer. You can change some things in the Properties window. When a field is disabled, it means you cannot modify it.

Accessing or Opening a FileIf you are using a text editor to write your code, you can use that editor to open a file. For example, in Notepad, you can click File -> Open... If you are trying to open a C# file, first change the file type combo box to All Files (*). Then locate the file from its folder and click Open.

If a file is a member of the current project, to open it:

On the main menu, click Window and click the name of the file

In the top section of the Code Editor, click the label that holds the name of the file

In the Solution Explorer, double-click the name of the file

If you are using Microsoft Visual C# 2010 Express or Microsoft Visual Studio, to open a file:

On the main menu, click File -> Open File...

On the Standard toolbar, click the Open File button 

Any of these actions would display the Open File dialog box. From there, locate the file from its folder and click it. When you click Open, Microsoft Visual Studio will try to open the file. If it can, it would display it inside its interface. If it cannot, it would start looking for a program in the computer that can open the file and hand the job to it. That application would then decide whether it can open the file or not. For example, if you try opening a video (a file with AVI, MOD or another video extension), Microsoft Visual Studio would launch the Windows Media Player that would then try to play the video.

Besides the Code Editor, the integrated development interface (IDE) of the Microsoft Visual C# 2010 is made of various parts, which we will review when necessary.

Solution and Project Management

Page 27: C# 4

IntroductionWe have seen how to create a console application. Microsoft Visual Studio allows you to create various other types of applications. This is why you should first display the New Project dialog box to select your option. Besides console applications, in future lessons, we will also learn how to create a library using the Class Library option.

Code SnippetsMicrosoft Visual C# ships with many skeleton codes you can use and customize. It writes the primary code for you and add all the necessary default behaviors. Once it has done this, you can change or remove any section. To access these code skeletons, in the section of the file where you want to add it, right-click and click Insert Snippet...:

In the menu that comes up, double-click Visual C#:

This would display a list of codes:

Page 28: C# 4

If you see the type of code you want to use, double-click it.

In some cases, if you have already written some code, you may want to change it or rather add some code to it. The Code Editor provides some skeleton codes you can use. To use this approach, right-click the code you want to modify and click Surround With... In the list that appears, double-click the desired option.

Code ColorsCode is written in a wide area with a white background. This is the area you use the keyboard to insert code with common readable characters. The Code Editor uses some colors to differentiate categories of words or lines of text.

The colors used are highly customizable. To change the colors, on the main menu, you can click Tools -> Options... In the Options dialog box, in the Environment section, click Fonts and Colors. To set the color of a category, in the Display Items section, click the category. In the Item Foreground combo box, select the desired color. If you want the words of the category to have a colored background, click the arrow of the Item Background combo box and select one:

Page 29: C# 4

In both cases, the combo boxes display a fixed list of colors. If you want more colors, you can click a Custom button to display the Color dialog box that allows you to "create" a color.

IndentationIndentation is another feature that makes your program easy to read. Indentation is a technique of grouping lines of code by category. To delimit the items of your code, you should indent them by two empty spaces or one tab. Indentation should be incremental. That is, when a line of code appears to be a child of the previous line, the new line should be indented.

To control the indentation of your code, on the main menu, click Tools -> Options... In the left list, expand C#, followed by Formatting and click Indentation. Then change the options on the right side:

Page 30: C# 4

After making the changes, click OK to validate or Cancel to ignore.

Saving a ProjectIf you are creating your application using a text editor, you must save your file(s) in a folder you will create.

In previous versions of Microsoft Visual C# (namely 2002 and 2003), you always had to formally create a project in order to use one and you always had to save it. After realizing that many of the projects that developers or students create are for experimental purposes, Microsoft provided the ability to only temporarily create a project, then to save it or not. Saving a project allows you to keep it on a medium so you can refer to it later.

When Microsoft Visual Studio 2010 (any edition) is installed, it creates a folder named Visual Studio 2010 in your Documents folder. The Documents folder is called your personal drive or your personal directory. Inside of the Visual Studio 2010 folder, it creates a sub-folder named Projects. By default, this is where it would save your projects, each with its own folder.

To save a project, on the Standard toolbar, you can click the Save All button  . Alternatively, on the main menu, you can click File -> Save All. If the project had already been saved but you want to save it under a different name, on the main menu, you can click File -> Save project name As...

Opening a ProjectThere are a various ways you can open an existing project:

On the main menu, click File -> Open Project...

On the Start Page, click Open Project...

Press Ctrl + Shift + O

A Solution

Page 31: C# 4

A solution is used to coordinate the different aspects of an application that is being created. When you create a project, it represents one detail of the application you have in mind. Besides the code you are writing, you may want to add other items. Instead of one project, in the next sections, we will see that a solution can contain more than one project.

When creating a project, the solution holds the same name as the project. You can see their names in the Solution Explorer:

The solution and a project can have different names. While working on a project, to rename the solution, in the Solution Explorer, you can click the first node, which is the name of the solution starting with Solution. Then, in the Properties window, click (Name) and type the name of your choice:

This name is temporary, especially if you have not yet saved the project. If you want to permanently save a solution for later use, there are two techniques you can use.

If you start saving a project for the first time, it would bring the Save Project dialog box. By default, Microsoft Visual Studio selects your personal directory as the path to the solution. This is called the location. In the location, Microsoft Visual Studio creates a folder as the solution of the project. The solution must have, or must be stored, in its own folder. As mentioned earlier, Microsoft Visual Studio uses the name of the project as the name of the solution. To rename the solution, you can change the string in the Solution Name text box. Remember that you can enter the name of the project in the Name text box. Here is an example:

Page 32: C# 4

When you save a project (for the first time), by default, Microsoft Visual C# creates a new folder for it in the My Documents\Visual Studio 2008\Projects folder. It uses the name of the solution to name the folder. It creates some files and stores them in that new folder. Then, it creates a sub-folder, using the name of the project, inside of the folder of the solution. Besides the sub-folder with the name as the project, it creates another folder namedanother folder namedDebug in the sub-folder of the name of the project. In each folder and some other folders, it creates some files that we will not pay attention to for now.

If the project had already been saved but you want to change the name of the solution, on the main menu, you can click File -> Save solution-name.sln As... This would bring the Save File As dialog box where you can specify the name of the solution and click Save.

Building a ProjectAfter creating a project and writing code, you may want to see the result. To do this, you must first build the application. This would create an executable.

To allow you to create programs, a computer language such as C# is equipped with an application named a compiler. A compiler is a computer program made of internal other sub-programs. One of the sub-programs, in fact probably the first, of a compiler is called a parser. A parser "scans" a file that contains (part of) the program. It checks the syntax, keywords, unknown words, and some other routines. If the parser finds a problem, which could be anything, either it stops or it continues making a list of the mistakes it found. Then it displays this list to you to fix. Sometimes it would point to the exact line where the/a problem was found. Sometimes it would point to the line where the problem showed its impact although the problem may be found somewhere else. With experience, you will know how to fix the programs or troubleshoot the problems.

If the parser doesn't find any problem, or after you have fixed the problems, it (the parser) passes its result(s) to the compiler. The compiler calls another program called a linker. If the program contains just one file, the linker considers it. If the program contains more than one file, the linker considers them. The linker gathers some of the files that the C# compiler shipped with (those files that your program needs in order to work, since your program doesn't need all possible files that the .NET Framework provides), puts them together ("links" them) with your file(s) to get your instructions in a manner that can produce a suitable result. If there is no significant problem, the compiler creates the program. This doesn't mean that everything is alright, it only means that the compiler thinks that everything is alright: it is still possible that the result may not be what you would expect. We will come back to these issues when studying debugging.

To make your life easier, all of the sub-programs (parser, linker, debugger, etc) that ship with C# are grouped in one large program: the compiler. Therefore, from now on, we will use the word "compiler" to refer to the program you use to "translate" your English instructions into a computer-based language.

The compiler that Microsoft created, and that we will use, that is, the compiler of the Microsoft .NET Framework is called csc. Like most other programs, it has the extension .exe. This csc name is not standard. This means that another C# compiler may have another name; csc.exe is just the name of the compiler we will use.

Page 33: C# 4

The csc compiler is freely available if you download the .NET Framework from the Microsoft web site.

To build an application at the Command Prompt, you use the csc.exe compiler. After download the .NET Framework, you must install it. What interests us is the csc.exe application. By default, it is installed in C:\Windows\Microsoft.NET\Framework\v4.0.21006.

You should add the path of the csc.exe to the Path of the Environment Variables. To start, use a file utility such as Windows Explorer and display the folder where csc is installed. Here is its path in Windows Explorer:

Select the path in the top combo box and copy it to the clipboard. Start the Control Panel. Click System and Security:

Page 34: C# 4

In the System and Security section, click System:

Page 35: C# 4

In the System window, click Change Settings. In the System Properties dialog box, click the Advanced tab. Click the Environment Variables button:

Page 36: C# 4

In the System Variables section, double-click Path or click it and click Edit:

Page 37: C# 4

Press the End key, type a semi-colon ";". Paste the value you had copied from the clipboard:

Click OK three times.

Open the Command Prompt. Type CD\ and press Enter to move to the root drive. Type CD and a space, followed by the folder (and sub-folder(s)) where the file is located, and press Enter. To compile, type csc followed by the name of the file and its extension:

Page 38: C# 4

The file produced from this operation has the extension .exe. By default, it holds same name as the file you had used. If you want to get an executable using a name of your choice, after csc, type /out:followed by the name you want, followed by a .exe extension, followed by a space, and followed by the name of the file you had created, with its extension. The formula to follow would be:

csc /out:NameOfExecutate.exe Filename.cs

The NameOfExecutate represents the name you want the executable to have. If the name you want is in one word, you can just type it. If you want a name made of various words, you can include those words in double-quotes.

If you are creating your application using a text editor and if you create many files, when compiling the project, you must remember to reference each file. To do that, in the last section, add the name of each file with its extension:

csc FileName1.cs FileName2.cs FileName_n.cs

The executable you get is the one you can use on other computers and that you can distribute to other people.

Executing a ProjectAfter building a project, you and your users can execute it. If you are working from the Command Prompt, to execute the project, type the name of the file that has the .exe extension and press Enter:

Page 39: C# 4

If you are working from Microsoft Visual C# 2010 Express or from Microsoft Visual Studio, to execute an application, on the main menu, you can click Debug -> Start Debugging.

Application: Executing an Application1. To execute the application, on the main menu, click Debug -> Start Debugging

2. After viewing the result in a DOS window, press Enter to close it

3. Close Microsoft Visual C# 2010 Express or Microsoft Visual Studio

4. When asked whether you want to save, click No