Chapter 29: .NET Framework Programming


This chapter introduces the .NET Framework and shows how to build .NET applications using both C# and Delphi for .NET languages. Besides learning the basics of the .NET framework and the C# language, you'll see how to create cross-language and Windows.Forms GUI applications and learn about differences between Windows.Forms and VCL.NET frameworks.

Overview of the .NET Platform

The .NET platform is a managed, language-independent platform for building and running applications. There are two primary components of the .NET platform: CLR and FCL, the Common Language Runtime and the .NET framework class library.

The Common Language Runtime

Standard Win32 applications, like VCL applications built with Delphi for Win32, are compiled to machine code that is directly executable on x86 processors. Unlike Win32 applications, .NET applications are compiled to what is known as CIL — the common intermediate language. The common intermediate language is also known as MSIL — Microsoft intermediate language.

To actually run a .NET application, the machine must have the .NET framework installed. The .NET framework is required because the CIL instructions of the .NET applications are JIT (just-in-time) compiled by the CLR to machine instructions when a .NET application is run.

The JIT compilation has advantages and disadvantages. The disadvantage of JIT compilation of .NET applications is that they execute a little slower than the equivalent Win32 applications the first time they are run.

The advantages of JIT compilation are:

  • The JIT compiler doesn't compile the entire application, but only the parts that have to be executed.

  • To improve the performance of .NET applications, the JIT compiler compiles a section of code only once and then caches the resulting native code.

  • Since the JIT compiler compiles the application on the client machine, not on the developer's machine, it could optimize code for the processor available on the client machine.

  • During the compilation of CIL to native code, the CIL code is verified for type safety, which ensures, among other things, that the code cannot access parts of memory that it isn't authorized to access.

Besides the JIT compilation, here are several more benefits of the CLR:

  • The CLR allows you to compile the application once to CIL and then run it on different CPUs and operating systems.

  • The CLR supports cross-language development (not only can you develop an application using several .NET languages, but you can also create a class in one language and inherit it in another one).

  • Objects created in .NET applications are managed by the CLR and garbage collected to prevent memory leaks. (The garbage collector also improves productivity because you don't have to worry about or write code to release objects from memory.)

The .NET Framework Class Library

The .NET framework class library (FCL) contains thousands of types divided into numerous namespaces. The FCL contains classes and data types that can and are used by developers that use any .NET language like Delphi for .NET, C#, VB.NET, Managed Extensions for C++, and JScript.NET.

The main namespace in the FCL is the System namespace. The System namespace contains all base classes of the .NET framework, including the root class of the framework, System.Object. The System.Object class is the base class for everything in the .NET framework, including primitive data types such as Boolean, integer, and string.

Table 29-1 shows several basic FCL types and their C# and Delphi for
.NET equivalents. C# and Delphi for .NET types are merely aliases that help you write more C-like code in C# and more Delphi-like and Delphi for Win32-compatible code in Delphi for .NET.

Table 29-1: Basic FCL data types

FCL Class Name

Delphi for .NET Type

C# Type

Byte

Byte

byte

Int16

Smallint

short

Int32

Integer

int

Int64

Int64

long

Single

Single

float

Double

Double

double

Boolean

Boolean

bool

String

String

string

Object

TObject

object

The biggest difference lies in the way the string type is interpreted in Delphi for Win32 and Delphi for .NET. In Delphi for Win32, the string type is an alias for the AnsiString type. In Delphi for .NET, the string type is an alias for the System.String type, which is a Unicode string type, like the WideString type in Delphi for Win32.

Besides the System namespace, there are several more commonly used FCL namespaces:

  • System.Data — Contains ADO.NET classes for accessing and manipulating data

  • System.Drawing — Contains GDI+ classes for creating graphical output

  • System.Collections — Contains lists, arrays, hashtables, and dictionaries

  • System.IO — Contains classes for file and stream I/O

  • System.Windows.Forms — Contains classes for building GUI applications

  • System.Web.UI — Contains basic ASP.NET classes

  • System.Web.UI.WebControls — Contains ASP.NET server controls

  • System.Web.Services — Contains classes for building web services

The VCL.NET Framework

The VCL.NET framework is the .NET version of the VCL framework, and is currently only available for Delphi developers. One of the biggest advantages of the VCL.NET framework over the FCL is that existing Win32 applications can be easily ported to .NET, with none or only minor changes to the code. The VCL.NET framework allows Delphi developers to continue using their Win32 Delphi experience and develop applications that can be, with a bit of help from the IFDEF compiler directive, recompiled to run on both Windows and Linux, and on the .NET platform.

By developing applications using VCL.NET you are not distancing yourself from the .NET framework; you can actually write Delphi applications that use both frameworks. For instance, Listing 29-1 shows a simple Delphi for .NET console application that uses the WriteLn procedure from the Delphi Runtime Library and the WriteLine method of the FCL's Console class (from the System namespace) to display text in the console window.

Listing 29-1: Using both the Delphi RTL and the FCL in a Delphi for .NET console application

image from book
program Project1; {$APPTYPE CONSOLE} uses SysUtils; begin   WriteLn('Delphi Runtime Library - WriteLn');   System.Console.WriteLine('FCL - Console.WriteLine');   Console.ReadLine; { FCL equivalent of the Readln procedure } end.
image from book

Both FCL and VCL.NET are implemented as a set of DLLs, which are like .NET executables, known as assemblies. The FCL assemblies, such as System.Drawing.dll and System.Windows.Forms.dll, are stored in the Windows directory, under Microsoft .NET\Framework\<Framework Version>\. VCL.NET assemblies, such as Borland.VclRtl.dll and Borland.VCL.dll, are stored in the Program Files directory, under Common Files\Borland Shared\BDS\Shared Assemblies\<Delphi Version>\.

VCL.NET assemblies can be used as external libraries or they can be, like packages, linked into the .NET executable to produce a "standalone" executable that only requires the .NET runtime to be installed on the target machine.



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net