AL MA TE RI .NET Architecture D Throughout this book, we emphasize that the C# language cannot be viewed in isolation, but must be considered in parallel with the .NET Framework. The C# compiler specifically targets .NET, which means that all code written in C# will always run within the .NET Framework. This has two important consequences for the C# language: The architecture and methodologies of C# reflect the underlying methodologies of .NET.
Chapter 1 The Relationship of C# to .NET C# is a relatively new programming language and is significant in two respects: ❑ It is specifically designed and targeted for use with Microsoft’s .NET Framework (a feature-rich platform for the development, deployment, and execution of distributed applications).
.NET Architecture You should note that the platform independence of .NET is only theoretical at present because, at the time of writing, a complete implementation of .NET is only available for Windows. However, a partial implementation is available (see, for example, the Mono project, an effort to create an open source implementation of .NET, at www.go-mono.com/). Performance improvement Although we previously made comparisons with Java, IL is actually a bit more ambitious than Java byte code.
Chapter 1 VB6 and this meant that VB6 was not a suitable language for running .NET programs. For example, VB6 is heavily integrated into COM and works by exposing only event handlers as source code to the developer — most of the background code is not available as source code. Not only that, it does not support implementation inheritance, and the standard data types Visual Basic 6 uses are incompatible with .NET. Visual Basic 6 was upgraded to Visual Basic .
.NET Architecture The compiler raises an error if you attempt to use features that are not supported by .NET on managed types (for example, templates or multiple inheritance of classes). You will also find that you will need to use nonstandard C++ features (such as the __gc keyword shown in the previous code) when using managed classes.
Chapter 1 Here are the important features of IL: ❑ Object orientation and use of interfaces ❑ Strong distinction between value and reference types ❑ Strong data typing ❑ Error handling through the use of exceptions ❑ Use of attributes The following sections take a closer look at each of these characteristics. Support for Object Orientation and Interfaces The language independence of .NET does have some practical limitations.
.NET Architecture done with COM as an intermediary. Not only that, but the COM architecture did not permit implementation inheritance, which meant that it lost many of the advantages of object-oriented programming. An associated problem was that, when debugging, you would still have to debug components written in different languages independently. It was not possible to step between languages in the debugger.
Chapter 1 circumstances in some of the languages that compile to managed code. Indeed, pointers (as opposed to references) are permitted only in marked blocks of code in C#, and not at all in Visual Basic (although they are allowed in managed C++). Using pointers in your code causes it to fail the memory type safety checks performed by the CLR. You should note that some languages compatible with .
.NET Architecture The CTS doesn’t merely specify primitive data types but a rich hierarchy of types, which includes welldefined points in the hierarchy at which code is permitted to define its own types. The hierarchical structure of the Common Type System reflects the single-inheritance object-oriented methodology of IL, and resembles Figure 1-1.
Chapter 1 Type Meaning Self-describing Types Data types that provide information about themselves for the benefit of the garbage collector (see the next section). Arrays Any type that contains an array of objects. Class Types Types that are self-describing but are not arrays. Delegates Types that are designed to hold references to methods. User-defined Reference Types Types that have been defined in source code and are stored as reference types. In C# terms, this means any class.
.NET Architecture We won’t go into the details of the CLS specifications here. In general, the CLS won’t affect your C# code very much, because there are very few non–CLS-compliant features of C# anyway. Garbage collection The garbage collector is .NET’s answer to memory management, and in particular to the question of what to do about reclaiming memory that running applications ask for.
Chapter 1 One important aspect of garbage collection is that it is not deterministic. In other words, you cannot guarantee when the garbage collector will be called; it will be called when the CLR decides that it is needed (unless you explicitly call the collector), though it is also possible to override this process and call up the garbage collector in your code. Security .
.NET Architecture Physical Memory PROCESS 1 Physical memory or disk space 4GB virtual memory PROCESS 2 Physical memory or disk space 4GB virtual memory Figure 1-2 In general, any process is able to access memory only by specifying an address in virtual memory — processes do not have direct access to physical memory. Hence it is simply impossible for one process to access the memory allocated to another process.
Chapter 1 PROCESS - 4GB virtual memory APPLICATION DOMAIN: an application uses some of this virtual memory APPLICATION DOMAIN: another application uses some of this virtual memory Figure 1-3 If different executables are running in the same process space, they are clearly able to easily share data, because theoretically they can directly see each other’s data.
.NET Architecture The architecture of exception handling also provides a convenient means to pass an object containing precise details of the exception condition to an exception handling routine. This object might include an appropriate message for the user and details of exactly where in the code the exception was detected.
Chapter 1 An important characteristic of assemblies is that they contain metadata that describes the types and methods defined in the corresponding code. An assembly, however, also contains assembly metadata that describes the assembly itself. This assembly metadata, contained in an area known as the manifest, allows checks to be made on the version of the assembly, and on its integrity. ildasm, a Windows-based utility, can be used to inspect the contents of an assembly, including the manifest and metadata.
.NET Architecture Shared Assemblies Shared assemblies are intended to be common libraries that any other application can use. Because any other software can access a shared assembly, more precautions need to be taken against the following risks: ❑ Name collisions, where another company’s shared assembly implements types that have the same names as those in your shared assembly. Because client code can theoretically have access to both assemblies simultaneously, this could be a serious problem.
Chapter 1 The great thing about the .NET base classes is that they have been designed to be very intuitive and easy to use. For example, to start a thread, you call the Start() method of the Thread class. To disable a TextBox, you set the Enabled property of a TextBox object to false.
.NET Architecture Namespaces Namespaces are the way that .NET avoids name clashes between classes. They are designed to avoid the situation in which you define a class to represent a customer, name your class Customer, and then someone else does the same thing (a likely scenario — the proportion of businesses that have customers seems to be quite high).
Chapter 1 type-checking. Specifically, if you are using VBScript and want to implement error handling in your pages, you have to use the On Error Resume Next statement, and follow every component call with a check to Err.Number to make sure that the call had gone well. ASP.NET is a complete revision of ASP that fixes many of its problems. It does not replace ASP; rather, ASP.NET pages can live side by side on the same server with legacy ASP applications. Of course, you can also program ASP.
.NET Architecture Web Forms To make Web page construction even easier, Visual Studio 2005 supplies Web Forms. They allow you to build ASP.NET pages graphically in the same way that Visual Basic 6 or C++ Builder windows are created; in other words, by dragging controls from a toolbox onto a form, then flipping over to the code aspect of that form, and writing event handlers for the controls. When you use C# to create a Web Form, you are creating a C# class that inherits from the Page base class, and an ASP.
Chapter 1 Creating Windows Forms Although C# and .NET are particularly suited to Web development, they still offer splendid support for so-called fat-client or thick-client apps, applications that have to be installed on the end user’s machine where most of the processing takes place. This support is from Windows Forms. A Windows Form is the .NET answer to a Visual Basic 6 Form. To design a graphical window interface, you just drag controls from a toolbox onto a Windows Form.
.NET Architecture When combined with ADO.NET, C# has the ability to access quickly and generically data stores such as SQL Server and Oracle databases. The returned datasets can easily be manipulated using the ADO.NET object model, and automatically render as XML for transport across an office intranet.
Chapter 1 related technologies, Windows Forms are still a viable option for creating a user interface with speed and ease. Just remember to factor your code so that the user interface logic is separate from the business logic and the data access code. Doing so will allow you to migrate your application to the browser at some point in the future if you need to do so.
.NET Architecture C# Source Code VB.NET Source Code COMPILATION ASSEMBLY containing IL CODE Language Interoperability through CTS and CLS ASSEMBLY containing IL CODE .