The most important benefit of managed code is programmer productivity. All other things being equal, the output of any process is limited by the scarcest resources. In software development, that resource is time, specifically developer time. Some people consider the .NET garbage collector as an important part of the improved productivity story, and correctly so. However, what is even more significant is the API organization. The organization of the large API into classes and namespaces helped create a level of discoverability that did not exist in the Win32 API.
Microsoft introduced its first version of the .NET Framework in July 2000 at a Professional Developers Conference held in Orlando, Florida. This was not the first time that the company had introduced a new technology to improve programmer productivity. At that time, for example, Microsoft Visual Basic® was at least ten years old. What made the introduction of .NET significant was the breadth and depth of what it provided. In one stroke, the company was providing a whole new set of tools and a new API for use in creating client and Web-based applications.
Benefits of Managed Code
The .NET Compact Framework provides a subset of the .NET Framework. At the same time, the Compact Framework inherited the structure and organization of the .NET Framework. This meant that a programmer familiar with the System.Windows.Forms namespace on the full framework would find many similarities in the Compact Framework. Of course, the Compact version did not have all the classes, properties, methods, or events, but the syntax for the elements which are present is identical.
Programmer productivity is improved in many ways. One way is the organization of namespaces so that you can easily discover all related classes. For example, after you know that user interface classes are located in the System.Windows.Forms namespace, it is easy to look in that one location to find most every type that you want. Programming interfaces are growing larger and more complex so that anything that helps programmers find what they are looking for automatically makes them more productive.
The world of .NET programming for both the .NET Framework and the .NET Compact Framework is a world of object-oriented programming (OOP). There are several benefits that are derived from this. One is support for encapsulation that means the designer of a class may choose to hide some elements away from the user of a class. This improves code robustness by keeping those things hidden which may change in future versions or which, for various reasons, must remain undocumented.
Another OOP feature is inheritance. Inheritance enables new code to be written that builds on existing code. Inheritance is used within the .NET Compact Framework itself, where many of the built-in classes are built through a long chain of inheritance. For example, the TextBox class has five base classes and two other classes which themselves inherit from TextBox. As provided in the MSDN documentation, here is the inheritance hierarchy for the TextBox class:
System..::.Object
System..::.MarshalByRefObject
System.ComponentModel..::.Component
System.Windows.Forms..::.Control
System.Windows.Forms..::.TextBoxBase
System.Windows.Forms..::.TextBox
System.Windows.Forms..::.DataGridTextBox
System.Windows.Forms..::.DataGridViewTextBoxEditingControl
Another important OOP feature is support for strong types. This feature increases code robustness by providing a set of rules for when and how one type can be converted into another type. This is unlike traditional C programming, where you can coerce one type to be another type. As convenient as casting may be, it also enabled for various kinds of type conversion errors to remain hidden. It is much more difficult to hide such problems in a .NET program, because you cannot even compile code which contains type errors.
And then there is the support that is provided by the garbage collector in the form of memory cleanup. Microsoft .NET garbage collector is not the first garbage collector ever created. Earlier examples of systems with garbage collection include SmallTalk, Visual Basic, and Java by Sun. To a programmer, the primary benefit of garbage collection is that you can "allocate and forget" and be fairly confident that the runtime system will find and reclaim memory when it is not needed.
The presence of garbage collection provides an important helper for programmers. But it does not completely take away from the need of programmers to be as efficient as possible. As an obvious example, you should avoid allocating many small (less than eight byte) objects because the overhead for each object is at least four bytes, and many 8-byte objects would therefore incur a built-in cost of 50 percent for each object allocated. Finding some way to combine small objects would make memory use more efficient (for example, putting the previously mentioned small objects into one or more arrays).
The garbage collector also cannot help when you are allocating native objects from managed code. For example, if you allocate native memory and pass that memory to a native Win32 function, the garbage collector cannot help you. Instead, you must follow whatever cleanup rules apply to that specific Win32 function.
To help with such situations, the architects of .NET did provide the Dispose and the Finalize methods to enable cleanup under special circumstances. In other words, the garbage collector itself provides no help for cleaning up native objects. But a managed object that contains one or more native object can take advantage of special features to perform the required cleanup of the nested Win32 objects.
.NET Compact Framework Features
Undoubtedly the productivity aspects of .NET are appealing. But the general design of an API does not guarantee its suitability for specific applications. For that, you must examine the API and the specific features that it supports. Generally, the ideal Compact Framework application has a forms-based user interface, works with many record-oriented data, and communicates with client or server systems over a network.
This broad description is not meant to define the ideal application, but one that takes advantages of some significant features of the API. What about other applications that do not exactly match this general description? For those, you can match your feature needs to the general feature set for what is supported in the Compact Framework. Tables 2-4 provide a summary of the features that are built into .NET Compact Framework 3.5. These tables were created by using namespace and class information that are available through the Object Browser feature of Visual Studio 2008.
Table 2 summarizes what might be called "core .NET support". The classes in these namespaces provide the foundation for building applications. The base types on the desktop are the same as the base types found on devices. Devices have a hierarchical file system and a hierarchical registry, exactly like on the desktop. Whereas device storage may be more limited than on the desktop, the .NET classes that access them are identical.
As expected in a graphical user-interface, you have built-in user interface objects that support rich text in the form of TrueType® fonts. Both device and desktop APIs enable rich graphical output, in the form of both vector and raster graphics. Devices are limited to being able to draw 2-dimensional, nonrotated graphics, whereas desktop systems support 3-dimensional graphics, with rich support for rotation.
Table 2 – Core .NET Support
|
Namespace
|
Comments
|
|---|
|
System
|
Base types
|
|
System.IO
|
File I/O
|
|
Microsoft.Win32
|
System registry
|
|
Microsoft.VisualBasic
|
Support for Visual Basic 6 compatibility helpers
|
|
System.Globalization
|
Localization classes
|
|
System.Collections
|
Collection support includes ArrayList, Hashtable, Queue, SortedList, and Stack
|
|
System.Drawing
|
Two-dimensional graphical output
|
|
System.Reflection
|
Ability to read and parse managed assemblies (but no support for generating code – Emit)
|
|
System.Runtime.InteropServices
|
Calling functions in Win32 DLLs and also COM interoperability
|
|
System.Text
|
Convert strings between character sets
|
|
System.Threading
|
Multithreaded programming
|
|
System.Security.Cryptography
|
Encryption
|
|
System.Text.RegularExpressions
|
Text parsing
|
|
System.Windows.Forms
|
Form-based user interface
|
Table 3 summarizes the communications support that is built into .NET Compact Framework namespaces. One late-comer to both desktop and device-side programming is support for serial communications (for example, "COM1:" "COM2:" ports). The first versions of the .NET Framework on both desktop and device omitted this support. However, they have managed to find their way into later frameworks.
As for the rest of the communications support, most support is network-enabled communications technologies. Whether you want to build your own protocols in addition to the low-level sockets, or take advantage of the built-in support for higher-level protocols, both kinds of support are available. At the high end of the network protocol stack is support for building Web Service clients, and support for using the latest Windows Communication Foundation (WCF) protocol.
Table 3 – Communication Support
|
Namespace
|
Comments
|
|---|
|
System.Net.Irda
|
Support for infrared communications
|
|
System.Messaging
|
Microsoft Message Queue (Message Queuing) support
|
|
System.ServiceModel
|
Windows Communication Foundation (WCF) support
|
|
System.Web.Services
|
Web service client support
|
|
Microsoft.ServiceModel.Channel.Mail
|
WCF service that uses e-mail as channel transport
|
|
System.IO.Ports
|
Serial communication
|
|
System.Net, System.Net.Sockets
|
Network programming
|
Table 4 summarizes the namespaces with data management support. For programmers who prefer to have data that is stored in rows and columns, there is ADO.NET. This API makes it easy to transfer data between memory and a relational database, such as Microsoft SQL Server® Mobile Edition. ADO.NET also enables transfer of data to XML, for those situations when you are looking for a non-proprietary format for exchanging data with other systems. Or if you prefer, there are several managed code solutions for working with raw XML itself.
Table 4 – Data Management Support
|
Namespace
|
Comments
|
|---|
|
System.Data
|
ADO.NET - relational data sets in RAM.
|
|
System.Linq
|
Language integrated query (LINQ) support for manipulating 1-and 2-dimensional data arrays. This includes query, filtering, and arithmetic operations.
|
|
System.Xml.Linq
|
LINQ support for XML.
|
|
System.Xml.Serialization
|
XML serialization.
|
|
System.Xml.Schema
|
XML schema.
|
|
System.Runtime.Serialization
|
XML serialization.
|
|
System.Xml
|
XML manipulation.
|
Table 5 summarizes Windows-Mobile-specific classes which are found in the .NET Compact Framework. As suggested by these classes, Microsoft is more than willing to extend its managed code library to support application programming. At the same time, be aware that the use of these classes prevents an assembly from being able to run on the desktop. If portability between device and desktop is important to you, you just have to structure your code to take these device-dependent classes into account. One solution might involve putting all such dependencies into one DLL, and then replacing that DLL when it runs on the desktop.
Table 5 – Windows Mobile-Specific .NET Support
|
Namespace
|
Class
|
Comment
|
|---|
|
Microsoft.WindowsCE.Forms
|
DocumentList
|
Wrapper for Pocket PC document list control.
|
|
Microsoft.WindowsCE.Forms
|
HardwareButton
HardwareKeys
|
Support for Pocket PC hardware buttons.
|
|
Microsoft.WindowsCE.Forms
|
InputPanel
|
Controls on-screen keyboard (Software input panel) on Pocket PC and Windows CE.
|
|
Microsoft.WindowsCE.Forms
|
InputMode
|
Controls input mode on Smartphone keypads.
|
|
Microsoft.WindowsCE.Forms
|
LogFont
|
Full access to underlying GDI font capabilities.
|
|
Microsoft.WindowsCE.Forms
|
MessageWindow
|
Wrappers for Win32 window in managed code.
|
|
Microsoft.WindowsCE.Forms
|
Notification
|
Support for notifications.
|
|
Microsoft.WindowsCE.Forms
|
ScreenOrientation
|
Screen rotation support.
|
|
Microsoft.WindowsCE.Forms
|
SystemSettings
|
Pocket PC-specific settings.
|
|
Microsoft.WindowsMobile.DirectX
Microsoft.WindowsMobile.Direct3D
|
GraphicsStream
|
Windows Mobile support for DirectX 3-D graphical display.
|
|
Microsoft.WindowsMobile.Configuration
|
ConfigurationManager
|
Windows Mobile support for configuration manager. In the full framework, this is deployed in the System.Configuration namespace.
|
|
Microsoft.WindowsMobile.PocketOutlook
|
Appointment
Contact
EmailMessage
SmsMessage
Task
|
Enables access to Outlook Mobile data. This includes appointments, contacts, e-mail, SMS messages, tasks.
|
|
Microsoft.WindowsMobile.Forms
|
CameraCaptureDialog
ChooseContactDialog
SelectPictureDialog
|
Provides dialog boxes for camera support, picture viewing, and contact selection.
|
|
Microsoft.WindowsMobile.Status
|
RegistryState
SystemState
|
Managed wrappers for the State and Notification Broker.
|
|
Microsoft.WindowsMObile.Telephony
|
Phone
PHoneMakeCallInfo
|
Support for making phone calls.
|