Building C# Applications: The C# Command-Line Compiler (CSC - Exe)
Building C# Applications: The C# Command-Line Compiler (CSC - Exe)
VIK
BUILDING C# APPLICATIONS
The C# Command-Line Compiler (csc.exe)
Configuring the C# Command-Line Compiler
To equip your development machine to compile *.cs files from any directory, follow
these steps (which assume a Windows XP installation; Windows NT/2000 steps will
differ slightly):
1. Right-click the My Computer icon and select Properties from the pop-up menu.
2. Select the Advanced tab and click the Environment Variables button.
3. Double-click the Path variable from the System Variables list box.
4. Add the following line to the end of the current Path value (note each value in the
Path variable is separated by a semicolon):
C:\Windows\Microsoft.NET\Framework\v2.0.50215
2
C# PROGRAMMING AND .NET
VIK
Once you have finished, save the file in a convenient location (e.g., C:\CscExample) as
TestApp.cs.
Each possibility is represented by a specific flag passed into csc.exe as a command-line
parameter see below table which are the core options of the C# compiler.
csc TestApp.cs
TestApp.exe can now be run from the command line a shows o/p as;
C:\TestApp
Testing! 1, 2, 3
3
C# PROGRAMMING AND .NET
VIK
At the command line, you must inform csc.exe which assembly contains the
used namespaces.
Given that you have made use of the MessageBox class, you must specify the
System.Windows.Forms.dll assembly using the /reference flag (which can be
abbreviated to /r):
4
C# PROGRAMMING AND .NET
VIK
5
C# PROGRAMMING AND .NET
VIK
When you specify /bugreport, you will be prompted to enter corrective information
for the possible error(s) at hand, which will be saved (along with other details)
into the file you specify.
For example:
When you recompile this file using the /bugreport flag, you are prompted to enter
corrective action for the error at hand in command prompt.
If you were to open the resulting *.txt file you would find a complete report
regarding this compilation cycle And shows message as the ; expected.
6
C# PROGRAMMING AND .NET
VIK
7
C# PROGRAMMING AND .NET
VIK
C# response files contain all the instructions to be used during the compilation of
current build or project. By convention, these files end in a *.rsp (response)
extension.
Assume that you have created a response file named TestApp.rsp that contains
the following arguments (as you can see, comments are denoted with the #
character):
# This is the response file for the TestApp.exe app
# External assembly references.
/r:System.Windows.Forms.dll
# output and files to compile (using wildcard syntax).
Now, assuming this file is saved in the same directory as the C# source code
files to be compiled, you are able to build your entire application as follows (note
the use of the @ symbol):
csc @TestApp.rsp
Also note that flags listed explicitly on the command line before a response file
will be overridden by the specified *.rsp file. Thus, if you were to enter
8
C# PROGRAMMING AND .NET
VIK
Note The /reference flag is cumulative. Regardless of where you specify external
assemblies (before, after, or within a response file) the end result is a summation of
each reference assembly.
When you are building your C# programs using csc.exe, this file will be
automatically referenced, even when you supply a custom *.rsp file.
Given the presence of the default response file, the current TestApp.exe
application could be successfully compiled using the following command set (as
System.Windows.Forms.dll is referenced within csc.rsp):
In the event that you wish to disable the automatic reading of csc.rsp, you can
specify the /noconfig option:
9
C# PROGRAMMING AND .NET
VIK
This tool provides dozens of options that allow you to debug your assembly.
You may view them by specifying the /? flag:
cordbg /?
Before you can debug your application using cordbg.exe, the first step is to
generate debugging symbols for your current application by specifying the
/debug flag of csc.exe.
For example, to generate debugging data for TestApp.exe, enter the following
command set:
This generates a new file named (in this case) testapp.pdb. If you do not have an
associated *.pdb file, it is still possible to make use of cordbg.exe; however, you
will not be able to view your C# source code during the process.
10
C# PROGRAMMING AND .NET
VIK
Once you have generated a *.pdb file, open a session with cordbg.exe by
specifying your .NET assembly as a command-line argument (the *.pdb file will
be loaded automatically):
cordbg.exe testapp.exe
Now debugging mode and may apply any number of cordbg.exe flags at the (cordbg)
command prompt:
When you wish to quit debugging with cordbg.exe, simply type exit (or shorthand ex).
This product allows you to build applications using any number of .NET-aware
(and unaware) languages. Thus, you are able to use VS .NET when building C#,
J#, VB .NET, MFC, ATL (4.0) or traditional C-based Win32 applications.
The one thing you cannot do is build a traditional Visual Basic 6.0 application
using VS .NET.
If you want to create classic VB 6.0 COM servers (or any additional VB 6.0
project types) you need to make use of the Visual Basic 6.0.
11
C# PROGRAMMING AND .NET
VIK
By default, the first thing you see when you launch Visual Studio .NET is the
Start Page. For the present, you need only be concerned with the Projects and
My Profile tabs (the remaining options allow you to connect to the Internet to view
online resources, obtain product updates, and whatnot).
The Projects view allows you to open existing projects as well as create a brandnew project workspace. Be aware that these options are also available via the
File menu.
When you click the My Profile tab, Here, you are able to control how the VS .NET
IDE should be configured each time you launch the tool.
For example, the Keyboard Scheme drop-down list allows you to control which
keyboard mapping should be used. If you want, you can opt to have your
shortcut keys configured to mimic VB 6.0, Visual C++ 6.0, or the default VS .NET
settings.
Other options allow you to configure how online Help should be filtered (and
displayed), as well as how the core IDE windows (i.e., Properties, Toolbox, etc.)
should dock themselves. To check things out first-hand, take a moment to select
the various options found under the Window Layout drop-down list and find a
look and feel you are comfortable with.
Finally, be aware that if you close the Start Page window (and want to get it
back), access the Help | Show Start Page menu option.
Open the New Project dialog box by clicking the New Project button from the Start Page,
or by choosing the File | New | Project menu selection. In figure, project types are
grouped (more or less) by language.
12
C# PROGRAMMING AND .NET
VIK
13
C# PROGRAMMING AND .NET
VIK
VS.NET logically arranges a given project using a solution metaphor. Simply put,
a "solution" is a collection of one or more "projects."
Each project contains any number of source code files, external references, and
resources that constitute the application as a whole.
The *.sln file can be opened using VS .NET to load each project in the
workspace. Using the Solution Explorer window, you are able to view and open
any such item. Notice the default name of your initial class is "Class1.cs."
14
C# PROGRAMMING AND .NET
VIK
Solution explorer window provides a Class View tab, which shows the object
oriented view of your project. One nice feature of this perspective is that if you
double click on a given icon, you are launched to the definition of the given type
member.
15
C# PROGRAMMING AND .NET
VIK
When you right-click a given item, you activate a context-sensitive pop-up menu. The
menu lets you access a number of tools that allow you to configure the current project
settings and sort items in a variety of ways. For example, right-click the Solution node
from Class View and check out below figure
Configuring a C# Project
In the Solution Explorer tab. Right-click the VS TestApp project node, and select
Properties from the context menu. This launches the all-important Project
Property Page.
That dialog box provides a number of intriguing settings, which map to various
flags of the command line compiler. To begin, when you select the General node,
you are able to configure the type of output file that should be produced by
csc.exe.
You are also able to configure which item in your application should be marked
as the "Startup object" (meaning, the class in the application that contains the
Main() method).
16
C# PROGRAMMING AND .NET
VIK
By default, this value is set to Not Set, which will be just fine unless your
application happens to contain multiple class types that define a method named
Main() (which will seldom, if ever, be the case).
Finally, notice that the General node also allows you to configure the 'default'
namespace for this particular project (when you create a new project with VS
.NET, the default namespace is the same as your project name).
This window allows you to interact with a number of characteristics for the item
that has the current focus. This item may be an open source code file, a GUI
widget, or the project itself.
For example, to change the name of your initial *.cs file, select it from the
Solution Explorer and configure the FileName property
You can change the name of your initial class, select the Class1 icon from
ClassView and edit the Properties window accordingly.
17
C# PROGRAMMING AND .NET
VIK
18
C# PROGRAMMING AND .NET
VIK
Now, update your existing main() method to create a new instance of this type, and call
the sayhi() member:
Once you have done so, you are able to run your new VS .NET application using the
Debug | Start Without Debugging menu option.
19
C# PROGRAMMING AND .NET
VIK
Visual Studio .NET contains an integrated debugger, which provides the same
functionality of cordbg.exe using a friendly user interface. To illustrate the basics,
begin by clicking in the far left gray column of an active code window to insert a
breakpoint.
When you initiate a debug session (via the Debug | Start menu selection), the
flow of execution halts at each breakpoint.
Using the Debug toolbar (or the corresponding keystroke commands), you can
step over, step into, and step out of a given function.
When you run an application, you are instructing VS .NET to ignore all
breakpoints, and to automatically prompt for a keystroke before terminating the
current console window.
If you debug a project that does not have any breakpoints set, the console
application terminates so quickly that you will be unable to view the output.
20
C# PROGRAMMING AND .NET
VIK
One extremely useful aspect of Visual Studio .NET is the Server Explorer
window, which can be accessed using the View menu.
Using the Server Explorer, you can attach to and manipulate local and remote
databases (and view any of the given database objects), examine the contents of
a given message queue, and obtain general machine-wide information (such as
seeing what services are running and viewing information in the Windows event
log).
21
C# PROGRAMMING AND .NET
VIK
The object browser allows you to view the namespaces, types, and type
members of each assembly referenced by the current solution.
22
C# PROGRAMMING AND .NET
VIK
Integrated database support is also part of the VS .NET IDE. Using the Server
Explorer window, you can open and examine any database object from within the
IDE. For example, below figure shows a view of the Inventory table of the Cars
database you build during our examination of ADO.NET.
Integrated Help
The .NET documentation is extremely good, very readable, and full of useful
information. Given the huge number of predefined .NET types (which number
well into the thousands) you must be willing to roll up your sleeves and dig into
the provided documentation.
If you resist, you are doomed to a long, frustrating, and painful existence. To
prevent this from happening, VS .NET provides the Dynamic Help window, which
changes its contents (dynamically!) based on what item (window, menu, source
code keyword, etc.) is currently selected.
For example, if you place the cursor on Main() method, the Dynamic Help
window displays what's shown in figure below
23
C# PROGRAMMING AND .NET
VIK
C# "Preprocessor" Directives
C# supports the use of various symbols that allow you to interact with the
compilation process.
24
C# PROGRAMMING AND .NET
VIK
The most interesting of all preprocessor directives are #region and #endregion.
Using these tags, you are able to specify a block of code that may be hidden
from view and identified by a friendly textual marker.
The use of regions can help keep lengthy *.cs files more manageable. For
example, you could create one region for a type's constructors, another for type
properties, and yet another for internal helper classes.
The following class has nested two internal helper types, which have been
wrapped in a region block:
When you place your mouse cursor over a collapsed region, you are provided with a
snapshot of the code lurking behind.
25
C# PROGRAMMING AND .NET
VIK
The preprocessor directives (#if, #elif, #else, #endif) allows you to conditionally
compile a block of code, based on predefined symbols.
The classic use of these directives is to include additional (typically diagnosticcentric) code only compiled under debug builds.
For example, that when your current application is configured under a debug
build, you wish to dump out a number of statistics to the console:
26
C# PROGRAMMING AND .NET
VIK
Here, we are checking for a symbol named DEBUG. If it is present, we dump out
a number of interesting statistics using some (quite helpful) static members of the
System.Environment class.
If the DEBUG symbol is not defined, the code placed between #if and #endif will
not be compiled into the resulting assembly, and effectively ignored.
If you wish to define this symbol on a file by file basis, you are able to make use
of the #define preprocessor symbol as follows (do note that the #define directive
must be listed before anything else in the *.cs source code file):
To the DEBUG symbol to apply for each *.cs file in your application, you can
simply select a Debug build of the current project using the IDE's Standard
toolbar or using the Build | Configuration Manager Menu selection.
27
C# PROGRAMMING AND .NET
VIK
28
C# PROGRAMMING AND .NET
VIK
29
C# PROGRAMMING AND .NET
VIK
30
C# PROGRAMMING AND .NET
VIK
IMPORTANT QUESTIONS
1. Explain how CSC.exe computer is used to build c# application. Explain any five
flags with appropriate examples.
6m
2. What is cordbg.exe? List and explain any five command line flags recognized by
crdbg.exe while running .Net assemblies under debug mode.
3. What is csc.rsp file? Where is it located?
7m
3m
II.
III.
Response files
IV.
10m
#region,#endregion
II.
5m
8m
Or
31
C# PROGRAMMING AND .NET
VIK
II.
OS version
III.
Logical drivers
IV.
host name
V.
.NET version
8m