| [ directory ] |
|
2.4 C# ToolsThis section explores some of the tools provided with the .NET SDK that can improve your C# programming productivity. 2.4.1 Visual Studio .NETMicrosoft provides an IDE for fast and productive development with C#. Visual Studio .NET is an IDE for doing .NET development with Visual Basic, Visual C++, and C#. Figure 2.1 shows the start screen of Visual Studio .NET. Figure 2.1. Microsoft Visual Studio .NET Start Screen
Typically, IDEs are helpful when you have a lot of files to compile, and so you would group them into projects. Therefore, let's start a new project. Step 1Click on the New menu option under the File menu item. Step 2Click on the Project submenu option. You will see a dialog box (Figure 2.2). Select the Visual C# option on the left side. On the right side you will see several kinds of applications that you can create using the IDE. Click on Console Application. Figure 2.2. Dialog Box Showing Project Types
Step 3Type the name of your new project in the Name textbox, and type the name of the location where you want to create the new project. For this example, type Project1 for the name, and type C:\WorkingFolder for the location. Figure 2.3 shows what you see next. Figure 2.3. Project1 Template for a Console Application
Step 4The IDE creates a template class that you can modify. The class file is named Class1.cs. This name is not very intuitive, so save the file as Hello.cs and delete the Class1.cs file from the C:\WorkingFolder\Project1 directory. Now change the name of the class to Hello. It is not necessary that the class name be the same as the file name, but as you will realize when you have large projects with lots of files, it's easier to associate classes with files if each file has only one class with the same name. Step 5The comments that you see above the class definition and the Main method are the C# equivalent of the Javadoc documentation file. This file is XML-based. Remove the TODO comment from the body of the Main method. Type the following line inside the Main method's body:
Console.WriteLine("Hello");
The final screen looks something like Figure 2.4. Figure 2.4. The Final Screen of the Project before Building
While typing the text in the window, you may notice pop-up boxes. This is the Visual Studio IntelliSense feature, which shows you the syntax of the construct as you are typing it. Step 6Now save your project by clicking on File and then Save All. Step 7Click on the Build menu and then click on the Build Project1 menu option. The IDE pops up a small window indicating the status of the build process, as shown in Figure 2.5. If you now explore the c:\Workingfolder\Project1 directory, you will see that a lot of files and directories have been created. The Project1.exe file is created in the c:\Workingfolder\Project1\bin\Debug directory. You can run this program from the command line now. To add more class files to the project, click on the Project menu and then on Add Class. To remove any class file from the current project, click on Project and then on Exclude from Project. Figure 2.5. The Final Screen of the Project after Building
Behind the scenes, all your project details are stored in an XML file called Project1.csproj. If you open the file using WordPad, you will notice that the various project files are listed. The IDE also creates an AssemblyInfo.cs file that essentially is a programmable instruction set for deploying the project. You will learn about assemblies and deployment in detail in Chapter 23. We will also discuss the IDE when it comes time to create new types of applications, such as class libraries, Webforms, Windows applications, and so on. 2.4.2 The Intermediate Language DisassemblerThe Intermediate Language Disassembler (ILDASM) can do the following:
To see the IL code generated for MySample.1.cs, you open the MySample.1.EXE file using the ildasm executable. The ildasm executable can be found in the following path: C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin If you have trouble finding the file, just do a search on the file name. After you start the ILDASM tool, click on File and then Open. Select the EXE file you want to view. The tool then displays the EXE file in a tree format. To see the IL of the individual tree components, highlight the component and then press Enter. Alternatively, you can do a tree dump of the file in an IL file. Click on File and then Dump. A dialog box pops up asking to save the dump in an IL file. Save the details in the Project1.il file under c:\Workingfolder\Projects\bin\debug. Listing 2.15 shows the contents of the dump file. The lines starting with // and /* and */ are comments, so you can edit them if you want to. The ILDASM can be called on the command line, too. Figure 2.6 shows the details of the Project1.IL file without the comments. Listing 2.15 Contents of the Dump File
// Microsoft (R) .NET Framework IL
Disassembler.Version 1.0.3512.0
// Copyright (C) Microsoft Corporation 1998?001.
All rights reserved.
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
// .z\V.4..
.ver 1:0:3300:0
}
.assembly Project1
{
.custom instance void
[mscorlib]System.Reflection.AssemblyCopyrightAttribute
::.ctor(string)
= ( 01 00 00 00 00 ) .custom instance void
[mscorlib]System.Reflection.AssemblyKeyFileAttribute
::.ctor(string)
= ( 01 00 00 00 00 )
.custom instance void
[mscorlib]System.Reflection.AssemblyDelaySignAttribute
::.ctor(bool)
= ( 01 00 00 00 00 )
.custom instance void
[mscorlib]System.Reflection.AssemblyTrademarkAttribute
::.ctor(string)
= ( 01 00 00 00 00 ) .custom instance void
[mscorlib]System.Reflection.AssemblyKeyNameAttribute
::.ctor(string)
= ( 01 00 00 00 00 )
.custom instance void [mscorlib]System.Reflection.
AssemblyProductAttribute
::.ctor(string)
= ( 01 00 00 00 00 ) .custom instance void
[mscorlib]System.Reflection.AssemblyCompanyAttribute
::.ctor(string)
= ( 01 00 00 00 00 )
.custom instance void
[mscorlib]System.Reflection.AssemblyConfigurationAttribute
::.ctor(string)
= ( 01 00 00 00 00 )
.custom instance void
[mscorlib]System.Reflection.AssemblyDescriptionAttribute
:.ctor(string)
= ( 01 00 00 00 00 ) .custom instance void
[mscorlib]System.Reflection.AssemblyTitleAttribute
::.ctor(string)
= ( 01 00 00 00 00 )
//? The following custom attribute is added automatically,
do not uncomment棗?
// .custom instance void
[mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(bool,
//bool) = ( 01 00 01 01 00 00 )
.hash algorithm 0x00008004
.ver 1:0:715:27376
}
.module Project1.exe
// MVID: {B4CFC431-2D46-4A50-99FF-63874717B63F}
.imagebase 0x00400000
.subsystem 0x00000003
.file alignment 512
.corflags 0x00000001
// Image base: 0x03650000
//
// ============== CLASS STRUCTURE DECLARATION =================
//
.namespace Project1
{
.class private auto ansi beforefieldinit Hello
extends [mscorlib]System.Object
{
} // end of class Hello
} // end of namespace Project1
// ======== GLOBAL FIELDS AND METHODS ===================
// =============== CLASS MEMBERS DECLARATION ==================
// note that class flags, 'extends' and 'implements' clauses
// are provided here for information only
.namespace Project1
{
.class private auto ansi beforefieldinit Hello
extends [mscorlib]System.Object
{
.method private hidebysig static void
Main(string[] args) cil managed
{
.entrypoint
.custom instance void [mscorlib]System.
STAThreadAttribute::.ctor()
= ( 01 00 00 00 )
// Code size 11 (0xb)
.maxstack 1
IL=_0000: ldstr "Hello"
IL=_0005: call void [mscorlib]System.Console::
WriteLine(string)
IL=_000a: ret
} // end of method Hello::Main
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 1
IL=_0000: ldarg.0
IL=_0001: call instance void [mscorlib]System.
Object::.ctor()
IL=_0006: ret
} // end of method Hello::.ctor
} // end of class Hello
//=============================================================
} // end of namespace Project1
//*********** DISASSEMBLY COMPLETE ***********************
// WARNING: Created Win32 resource file
C:\Workingfolder\Project1\bin\Debug\Project1.res
Figure 2.6. Project1.IL
The details of the IL language can be found in Ilinstrset.doc, which is in C\Program Files\Microsoft.NET\FrameworkSDK\Tool Developer Guide. Reading the screen dump of Figure 2.6, you can see that the Hello class has two methods. One is the constructor, which is automatically provided by the runtime for any class, and the other is the Main method. You can see that the Main method contains the entry point instruction indicating to the CLR that it is the entry point method. The next instruction of the Main method is ldstr, which loads the string literal into the local method stack. This is followed by a call to the framework class, System.Console. The last instruction is ret (which is the return of the Main method). |
| [ directory ] |
|