1. Computing
Send to a Friend via Email

An Introduction to the .NET Framework Class Library

Fast Forward to Delphi for .Net - Part V

By

Article originally written by Corbin Dunn (Borland R&D Software Engineer).

This is the fifth article in the series (I, II, III, IV, VI) of articles designed with one goal in mind: to provide a quick and dirty introduction to the world of .Net programming with Delphi.

The .NET Framework Class Library (FCL) consists of a series of classes, interfaces, and value types that can be used to program with. The .NET Framework has types that allow one to easily:

  • Create extravagant graphical user interface applications
  • (System.Windows.Forms)
  • Access and manipulate data in various database formats (System.Data and System.Xml)
  • Dynamically query type information (System.Reflection)
  • Perform basic Input/Output operations (System.IO)
  • Perform operating system security checks (System.Security)
  • Create internet enabled applications (System.Net and System.Net.Sockets)
  • Create dynamic web based applications - also known as ASP.NET (System.Web)
  • Access basic data types, event handlers, and exceptions (System)

All types in the FCL are Common Language Specification (CLS) compliant. This allows any CLS compliant programming language to use the types, such as Delphi, C# or VB.

Basic FCL Namespaces and Types

Each class is arranged in a namespace that best describes its purpose, and the .NET Framework has a lot of namespaces. A basic fully qualified classname example would be System.Collections.ArrayList. The part to the left of the last dot is generally the namespace, while the part to the right of the last dot is the classname.
An exception to this rule has to do with nested types. For example, if you have a namespace MyNamespace, with a type Foo that contains a nested type Bar, the fully qualified name of Bar is MyNamespace.Foo.Bar. However, the namespace is just MyNamespace, not MyNamespace.Foo, which would be a classname.

All of the basic FCL classes are located in the root namespace System. Classes provided by third party developers are recommended to follow the namespace format of CompanyName.TechnologyName. For instance, Borland.Delphi.System is the fully qualified namespace for the Delphi System unit.

The FCL is based on a common type system. The common type system allows the framework to operate with multiple languages within a managed runtime.

Value Types versus Reference Types

Value types directly contain their data, and are generally allocated on the stack. Reference types store a reference to their data allocated on the heap. Value types generally are primitive types, such as basic ordinal types, while reference types are generally objects, such as System.Windows.Forms.Control. Since a value type contains a copy of its data, modifying a value type will only modify that particular instance. However, modifying a reference type will modify all instances that have a reference to that type.

For example, consider the following C# code using the value type System.Drawing.Point:

~~~~~~~~~~~~~~~~~~~~~~~~~
Point firstPoint = new Point(5, 5) ;
Point secondPoint = firstPoint;
firstPoint.X = 6;
~~~~~~~~~~~~~~~~~~~~~~~~~

On the second line, secondPoint is assigned the value of firstPoint; it receives a copy of the data in firstPoint, which in this case is the X value 5 and the Y value 5. On the third line, firstPoint.X is assigned a value of 6. This only modifies firstPoint.X; secondPoint.X still contains the value 5.

Consider the following C# code using a reference type, System.Windows.Forms.Control:

~~~~~~~~~~~~~~~~~~~~~~~~~
Control firstControl = new Control() ;
Control secondControl = firstControl;
firstControl.BackColor = Color.Red;
~~~~~~~~~~~~~~~~~~~~~~~~~

On the second line, secondControl is assigned the reference to firstControl. On the third line, modifying firstControl.BackColor also modifies secondControl.BackColor, since the type Control is a reference type.

In the FCL, all basic primitive types, such as an integer or boolean, are value types (including enumerations). Most other classes are generally reference types, including System.String. When programming, creating a struct in C# or a record in Delphi allows you to create a value type, while creating a class allows you to make a reference type.

Interface types

Interfaces are abstract declarations that are implemented by classes. One can cast from an interface to an object and vice versa (unlike Win32 Delphi).

We now move to Boxing and using Delegates to create event handlers...

©2014 About.com. All rights reserved.