Starting Microsoft Visual C# Express
Starting Microsoft Visual C# Express
Starting Microsoft Visual C# Express
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.
To launch Microsoft Visual Studio 2010, you can click Start -> (All) Programs -> Microsoft Visual
Studio 2010.
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:
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
i. After making the selection, click OK
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:
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:
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
becomes this
In the same way, you can make two or more windows share the same area.
Tabbed Windows
You 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
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 Language
A 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 chara
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
cell phone, a microwave oven, a car, the roller coaster of an amusement park, a health care machine, or a p
languages deal with specific hardware or some instructions can target a specific environment, such as a ce
environment uses a specific operating system that cannot be carried from one machine to another. Some o
hardware they are meant to serve.
Some languages are used to solve a specific type of problem and they cannot perform general assignments.
on the Internet are meant to display things on a browser. Such languages cannot perform arithmetic or geo
languages are used to operate on specific values, such as true or false values. Some other languages are use
problems. Languages in this latter category are probably the most regularly used. These are refferred to as g
C#, pronounced c sharp, is a general purpose programming language used to create instructions for the co
assist a user with performing an assignment. C# is a universal language that is used on many operating sys
Windows. C# is one of the languages used in the Microsoft .NET Framework. The Microsoft .NET Framew
or draw things on the computer.
Introduction to C# Projects
A computer project is a group of files and objects that contain the necessary instructions for the intended a
results. A C# project is made of one or more files that contain the instructions of the project. A simple C# p
holds all the necessairy instructions. An advanced project would contain more than that. To narrow our go
the C# language. The language is very huge so much that we may need more than fourty lessons just to cov
discouraged. This language is fun and helps you understand many things about computer programming. A
language. So we will learn scratch.
Introduction
Console Applications
The C# language is used to create applications that display on a black window referred to as the DOS prom
types of applications we will create in our lessons. To start such an application, you can write the instructio
Notepad or you can use a programming environment such as Microsoft Visual Studio or Microsoft Visual C
assume that you have access to one of those. We also assume that you have installed the programming env
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 Stu
Visual C# 2010 Express:
To launch Microsoft Visual Studio 2010, you can click Start -> (All) Programs -> Microsoft Visual Studio 2
5. Click OK
Comments
A comment is a line or paragraph of text that will not be considered as part of your code. There are two typ
To display a comment on a line of text, start the line with two forward slashes //. Anything on the right sid
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 com
between this combination of /* and */ would not be read. Therefore, you can use this technique to span a c
While you can manually create comments, Microsoft Visual Studio provides a tool that can assist you. To a
anything on that line. Then, on the Standard toolbar, click the Comment Out the Selected Lines button
adjacent lines, select text on those lines and click the Comment Out the Selected Lines button . To remo
select text on the lines. Then, on the Standard toolbar, click the Uncomment the Selected Lines button
Managing Files
Introduction
In Microsoft Visual C# 2010 Express and in Microsoft Visual Studio, when a project is made of various file
in the top section of the Code Editor. Here are examples:
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
node, you can either click its button or double-click its name. To collapse a node, either click its button or
The root of the list is the name of the solution. Under the root is the name of the current project. If the solu
project, the name of each project is represented under the solution. Inside of the project are its folders, file
under a project name is References. After the References node, there are the names of the classes that are
The fourth part of the Solution Explorer is its tab.
Introduction
We have seen how to create a console application. Microsoft Visual Studio allows you to create various oth
you should first display the New Project dialog box to select your option. Besides console applications, in f
how to create a library using the Class Library option.
Code Snippets
Microsoft Visual C# ships with many skeleton codes you can use and customize. It writes the primary code
default behaviors. Once it has done this, you can change or remove any section. To access these code skele
you want to add it, right-click and click Insert Snippet...:
In the menu that comes up, double-click Visual C#:
Code Colors
Code is written in a wide area with a white background. This is the area you use the keyboard to insert cod
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 -> O
in the Environment section, click Fonts and Colors. To set the color of a category, in the Display Items sect
Foreground combo box, select the desired color. If you want the words of the category to have a colored ba
Item Background combo box and select one:
In both cases, the combo boxes display a fixed list of colors. If you want more colors, you can click a Custo
box that allows you to "create" a color.
Indentation
Indentation is another feature that makes your program easy to read. Indentation is a technique of groupi
delimit the items of your code, you should indent them by two empty spaces or one tab. Indentation shoul
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
click Indentation. Then change the options on the right side:
After making the changes, click OK to validate or Cancel to ignore.
Saving a Project
If 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
always had to save it. After realizing that many of the projects that developers or students create are for ex
provided the ability to only temporarily create a project, then to save it or not. Saving a project allows you
refer to it later.
When Microsoft Visual Studio 2010 (any edition) is installed, it creates a folder named Visual Studio 2010
Documents folder is called your personal drive or your personal directory. Inside of the Visual Studio 2010
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
All. If the project had already been saved but you want to save it under a different name, on the main men
name As...
Opening a Project
There 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
A solution is used to coordinate the different aspects of an application that is being created. When you crea
of the application you have in mind. Besides the code you are writing, you may want to add other items. In
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 S
The solution and a project can have different names. While working on a project, to rename the solution, i
click the first node, which is the name of the solution starting with Solution. Then, in the Properties windo
of your choice:
This name is temporary, especially if you have not yet saved the project. If you want to permanently save a
techniques you can use.
If you start saving a project for the first time, it would bring the Save Project dialog box. By default, Micros
personal directory as the path to the solution. This is called the location. In the location, Microsoft Visual S
solution of the project. The solution must have, or must be stored, in its own folder. As mentioned earlier,
name of the project as the name of the solution. To rename the solution, you can change the string in the S
that you can enter the name of the project in the Name text box. Here is an example:
When you save a project (for the first time), by default, Microsoft Visual C# creates a new folder for it in th
2008\Projects folder. It uses the name of the solution to name the folder. It creates some files and stores t
creates a sub-folder, using the name of the project, inside of the folder of the solution. Besides the sub-fold
creates another folder named debug. It also creates another folder namedDebug in the sub-folder of the
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,
name.sln As... This would bring the Save File As dialog box where you can specify the name of the solution
Building a Project
After creating a project and writing code, you may want to see the result. To do this, you must first build th
executable.
To allow you to create programs, a computer language such as C# is equipped with an application named a
computer program made of internal other sub-programs. One of the sub-programs, in fact probably the fir
A parser "scans" a file that contains (part of) the program. It checks the syntax, keywords, unknown words
parser finds a problem, which could be anything, either it stops or it continues making a list of the mistake
to you to fix. Sometimes it would point to the exact line where the/a problem was found. Sometimes it wou
problem showed its impact although the problem may be found somewhere else. With experience, you wil
troubleshoot the problems.
If the parser doesn't find any problem, or after you have fixed the problems, it (the parser) passes its resul
calls another program called a linker. If the program contains just one file, the linker considers it. If the pr
the linker considers them. The linker gathers some of the files that the C# compiler shipped with (those fil
to work, since your program doesn't need all possible files that the .NET Framework provides), puts them
file(s) to get your instructions in a manner that can produce a suitable result. If there is no significant prob
program. This doesn't mean that everything is alright, it only means that the compiler thinks that everythi
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 group
compiler. Therefore, from now on, we will use the word "compiler" to refer to the program you use to "tran
into a computer-based language.
The compiler that Microsoft created, and that we will use, that is, the compiler of the Microsoft .NET Fram
other programs, it has the extension .exe. This csc name is not standard. This means that another C# com
name; csc.exe is just the name of the compiler we will use.
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
What interests us is the csc.exe application. By default, it is installed in C:\Windows\Microsoft.NET\F
You should add the path of the csc.exe to the Path of the Environment Variables. To start, use a file utility
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 an
In the System and Security section, click System:
In the System window, click Change Settings. In the System Properties dialog box, click the Advanced tab.
button:
In the System Variables section, double-click Path or click it and click Edit:
Press the End key, type a semi-colon ";". Paste the value you had copied from the clipboard:
Executing a Project
After building a project, you and your users can execute it. If you are working from the Command Prompt,
name of the file that has the .exe extension and press Enter:
If you are working from Microsoft Visual C# 2010 Express or from Microsoft Visual Studio, to execute an a
can click Debug -> Start Debugging.