Sunteți pe pagina 1din 9

1 Building C# Applications

Chapter 2 Building C# Applications


BUILDING C# APPLICATIONS: The Role of the Command Line Complier (csc.exe), Building C# Application using csc.exe, Working with csc.exe Response Files, Generating Bug Reports, Remaining C# Compiler Options, The Command Line Debugger (cordbg.exe), Using the Visual Studio .NET IDE, Other Key Aspects of the VS.NET IDE, C# Pre processor: Directives, An Interesting Aside: The System.Environment Class.

The Role of the Command Line Compiler (csc.exe)


You can create .NET assemblies using C# command line compiler, csc.exe, where csc stands for C Sharp Compiler. This tool is included with the .NET framework SDK. Some of the reasons to work with command line compiler are You might not have a copy of visual studio 2005. You plan to make use of automated build tools such as MSBuild or NAnt. You want to deepen your understanding of C#. When you use graphical IDEs to build applications, you are ultimately instructing csc.exe how to manipulate your C# input files.

Configuring the C# Command Line Compiler


To equip the development machine to compile *.cs files from any directory, follow these steps Right click on the My Computer icon and select properties from the popup menu. Select the Advanced tab and click the Environment Variable button. Double click the path variable from the System Variables list box. Add the following line to the end of the current path value: C:\Windows\Microsoft .NET\Framework\v2.0.50215 Make a test run by opening a new command line window and entering csc /?

Configuring Additional .NET Command Line Tools


You can add the additional path variables to the System Variable list box: C:\Program Files\Microsoft Visual Studio 8\SDK\V2.0\Bin You should now be able to run any .NET utility from any command window.

Building C# Applications using csc.exe


Open notepad and enter the following //A Simple C# Program using System; Class Test
Kaustubh S, Lecturer Dept of Information Science and Engg Acharya Institute of Technology, Bangalore

2 C# Programming and .NET

{ public static void Main(string[] args) { Console.WriteLine(Hello! World); Console.ReadLine(); } } Save the file in convenient location as Test.cs. In the command line, type csc Test.cs. An exe file is generated with name Test.exe. Some of the output centric options available in C# compiler are:

To specify the type of target, we can use csc /target:exe Test.cs. We can also use csc /t:exe Test.cs. Test.exe can now be run from the command line.

Referencing External Assemblies


Open another notepad and enter the following //Illustrating Referencing external assemblies using System.Windows.Forms; class Test { public static void Main(string[] args) { MessageBox.Show(Hello! World); } } Save the file and compile the file using csc /r:System.Windows.Forms.dll Test.cs.

Kaustubh S, Lecturer Dept of Information Science and Engg Acharya Institute of Technology, Bangalore

3 Building C# Applications

Compiling Multiple Source Files using csc.exe


You can compile your C# files by listing each input file explicitly: csc /r:System.Forms.dll test.cs hello.cs The C# compiler allows you to make use of the wildcard character to include all files contained in the project directory. csc /r:System.Forms.dll *.cs

Referencing Multiple External Assemblies


List each assembly using a semicolon-delimited list. csc /r:System.Forms.dll;System.Drawing.dll *.cs

Working with csc.exe Response Files


C# response files contain all the instructions to be used during the compilation of your current build. These files end in *.rsp extension. #This is the response file #for the Test.exe app #External Assembly reference /r:System.Windows.Forms.dll #Output and files to compile /target:exe /out:Test.exe *.cs Assuming that this file is saved in the same directory as the C# source code files to be compiled, you are able to build the entire application as follows csc @Test.rsp csc @Firstfile.rsp @Secondfile.rsp csc /out:MyCoolApp.exe @TestApp.rsp

The Default Response File


The C# compiler has an associated default response file csc.rsp, which is located in same directory as csc.exe. When you are building your C# programs using csc.exe, this file will automatically referenced, even when you supply a custom *.rsp file. Given the presence of default response file, the current file Test.exe application could successfully compiled using the following command set. csc /out:Test.exe *.cs In the event, you wish to disable the automatic reading of csc.rsp, you can specify the /noconfig option
Kaustubh S, Lecturer Dept of Information Science and Engg Acharya Institute of Technology, Bangalore

4 C# Programming and .NET

csc @Test.rsp /noconfig

The Command Line Debugger (corddg.exe)


Before you debug the application using cordbg.exe, the first step is generate debugging symbols for your current application by specifying the /debug flag. csc @test.rsp /debug

This generates a new file named test.pdb. Open a session with cordbg.exe by specifying the .NET assembly as command line argument. Cordbg.exe test.exe When you wish to quit debugging, type exit or ex.

C# Pre processor Directives

Specifying Code Regions


You can specify a block of code by using #region and #endregion, that may be hidden from view and identified by a friendly textual marker. class Car { private string tire; private string steering; #region Constructors public Car() { } public Car(string tire) { } #endregion }
Kaustubh S, Lecturer Dept of Information Science and Engg Acharya Institute of Technology, Bangalore

5 Building C# Applications

Conditional Code Compilation


#if, #elif, #else, #endif allows you to conditionally compile a block of code, based on predefined symbols. class Program { public static void Main() { #if Debug Console.WriteLine(App directory: +Environment.CurrentDirectory); #endif } } You can define your custom pre processor symbols by using #debug. #define AccessCode

The System.Environment Class


This class allows you to obtain a number of details regarding the operating system currently hosting the .NET application using various static members. Environment.OSVersion Gets the current OS version. Environment.CurrentDirectory Get the present working directory. Environment.GetLogicalDrives() Gets all the drives present in the computer. Environment.Version Gets the version of the .NET platform. Environment.MachineName Gets the name of the current machine Environment.NewLine Gets the new line symbol for the current environment Environment.ProcessorCount Returns the number of processors on the current machine. Environment.SystemDirectory Returns the full path to the system directory. Environment.UserName Returns the name of the entity that started this application.

Building .NET applications using Visual Studio 2005


Some of the features provided by visual studio are GUI designer Database manipulation tools Object and project browsing utilities An integrated help system Visual XML editors/designers Support for mobile device development

Kaustubh S, Lecturer Dept of Information Science and Engg Acharya Institute of Technology, Bangalore

6 C# Programming and .NET

Support for Microsoft Office development The ability to track changes for a given source document and view revisions Integrated support for code refactoring An XML based code expansion library Visual class design tools and object test utilities A code definition window

The Solution Explorer Utility

The solution explorer utility allows you to view the set of all content files and referenced assemblies that comprise the current project. The references folder of solution explorer displays the list of each assembly you have currently referenced. When you need to reference additional assemblies, right click the references folder and select add reference. Select the assembly from the resulting dialogue box. When you double click the icon named properties, you are presented with the enhanced project configuration editor.

The Class View Utility


The purpose of this utility is to show all of the types in your current project from an object oriented perspective. The top pane displays the set of namespaces and their types, while the bottom pane displays the currently selected types members.

Kaustubh S, Lecturer Dept of Information Science and Engg Acharya Institute of Technology, Bangalore

7 Building C# Applications

The Code Definition Window


This tool allowed you to type in the name of a .NET type and views its C# definition.

An enhanced version of this tool has been integrated with visual studio 2005. Place the mouse pointer over any type in your C# code file; you will be presented with the snapshot of the type. For example, if you click on the String in any method, you find the definition of System.String class type.

The Object Browser Utility


Visual studio 2005 provides a utility to investigate the set of referenced assemblies within your current project.

Kaustubh S, Lecturer Dept of Information Science and Engg Acharya Institute of Technology, Bangalore

8 C# Programming and .NET

Integrated Support for Code Refactoring


Refactoring is a formal and mechanical process to improve an existing code base. Visual studio 2005 does a great deal of automated refactoring. Using refactor menu, related keyboard shortcuts, smart tags and/or context sensitive mouse clicks, you can dramatically reshape your code with minimal bother.

Code Expansions and Surround with Technology


Visual studio 2005 has the capability to insert complex blocks of C# code using menu selections, context sensitive mouse clicks, and/or keyboard shortcuts. The number of available code expansions can be broken down into two main groups:
Kaustubh S, Lecturer Dept of Information Science and Engg Acharya Institute of Technology, Bangalore

9 Building C# Applications

Snippets: These templates insert common code blocks at the location of the mouse cursor. Surround with: These templates wrap a block of selected statements within a relevant scope. Right click on the blank line within your Main() method and activate the insert snippet menu. Once you select a given item, you will find the related code expanded automatically. If you right click and select surround with menu, you would be presented with a list of options.

The Visual Class Designer


Visual studio 2005 gives us the ability to design classes visually. The class designer utility allows you to view and relationships of the types in your project. You are able to visually add and remove members to or from a type and have the modifications reflected in the corresponding C# files.

Object Test Bench


Object test bench allows you to quickly create an instance of a class and invoke its members without the need to compile and run the entire application.

The Integrated Help System


Visual studio 2005 provides the dynamic help window, which changes its contents based on what item is currently selected. For example, if you place the cursor on the Console class, the dynamic help window displays a set of links regarding the System.Console type.

Kaustubh S, Lecturer Dept of Information Science and Engg Acharya Institute of Technology, Bangalore

S-ar putea să vă placă și