24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 1 Part I: The C# Language Chapter 1: .
4727c01.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 3 .NET Architecture 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.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 4 Part I: The C# Language 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).
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 5 Chapter 1: .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).
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 6 Part I: The C# Language is heavily integrated into Component Object Model (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 .
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 7 Chapter 1: .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.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 8 Part I: The C# Language 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.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 9 Chapter 1: .NET Architecture did not permit implementation inheritance, which meant that it lost many of the advantages of objectoriented 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.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 10 Part I: The C# Language 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 .
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 11 Chapter 1: .NET Architecture The CTS doesn’t specify merely 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 CTS reflects the single-inheritance object-oriented methodology of IL, and resembles Figure 1-1.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 12 Part I: The C# Language Type Meaning Interface Types Interfaces. Pointer Types Pointers. 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.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 13 Chapter 1: .NET Architecture you restrict your classes to exposing only CLS-compliant features, code written in any other compliant language can use your classes. The beauty of this idea is that the restriction to using CLS-compliant features applies only to public and protected members of classes and public classes.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 14 Part I: The C# Language objects that are not referred to are deemed to be no longer accessible from your code and can therefore be removed. Java uses a system of garbage collection similar to this. Garbage collection works in .NET because IL has been designed to facilitate the process. The principle requires that you cannot get references to existing objects other than by copying existing references and that IL be type safe.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 15 Chapter 1: .NET Architecture spaces. The idea is that each process has available 4GB of virtual memory in which to store its data and executable code (4GB is for 32-bit systems; 64-bit systems use more memory). Windows imposes an extra level of indirection by which this virtual memory maps into a particular area of actual physical memory or disk space.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 16 Part I: The C# Language divided into a number of application domains. Each application domain roughly corresponds to a single application, and each thread of execution will be running in a particular application domain (see Figure 1-3).
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 17 Chapter 1: .NET Architecture error condition (for example, a file not being found, or being denied permission to perform some operation). These conditions can be defined as narrowly or as widely as you want. The exception architecture ensures that when an error condition occurs, execution can immediately jump to the exception handler routine that is most specifically geared to handle the exception condition in question.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 18 Part I: The C# Language An assembly is completely self-describing and is a logical rather than a physical unit, which means that it can be stored across more than one file (indeed dynamic assemblies are stored in memory, not on file at all). If an assembly is stored in more than one file, there will be one main file that contains the entry point and describes the other files in the assembly.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 19 Chapter 1: .NET Architecture what software uses them. There is, therefore, less need to take security precautions because there is no risk, for example, of some other commercial software overwriting one of your assemblies with some new version of it (apart from the case where software is designed specifically to perform malicious damage). There are also no problems with name collisions.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 20 Part I: The C# Language .NET Framewor k Classes Perhaps one of the biggest benefits of writing managed code, at least from a developer’s point of view, is that you get to use the .NET base class library. The .NET base classes are a massive collection of managed code classes that allow you to do almost any of the tasks that were previously available through the Windows API. These classes follow the same object model IL uses, based on single inheritance.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 21 Chapter 1: .NET Architecture ❑ File system and registry access (see Chapter 24, “Manipulating Files and the Registry”) ❑ Networking and Web browsing (see Chapter 35, “Accessing the Internet”) ❑ .NET attributes and reflection (see Chapter 12, “Reflection”) ❑ Access to aspects of the Windows OS (environment variables and so on; see Chapter 19, “.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 22 Part I: The C# Language Creating ASP.NET Applications Active Server Pages (ASP) is a Microsoft technology for creating Web pages with dynamic content. An ASP page is basically an HTML file with embedded chunks of server-side VBScript or JavaScript. When a client browser requests an ASP page, the Web server delivers the HTML portions of the page, processing the server-side scripts as it comes to them.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 23 Chapter 1: .NET Architecture richer user interface but are harder to maintain because they run on so many different machines. For this reason, people have relied on form-based applications when rich user interfaces were a necessity and extensive support could be provided to the users. Web Forms To make Web page construction even easier, Visual Studio 2005 supplies Web Forms. They allow you to build ASP.
727c01.qxd:WroxPro 5/7/07 12:12 PM Page 24 Part I: The C# Language 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.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 25 Chapter 1: .NET Architecture It is very easy to write services in C#. .NET Framework base classes are available in the System .ServiceProcess namespace that handle many of the boilerplate tasks associated with services, and in addition, Visual Studio .NET allows you to create a C# Windows Service project, which uses C# source code for a basic Windows service. Chapter 22, “Windows Services,” explores how to write C# Windows Services.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 26 Part I: The C# Language To create an enterprise application with C#, you create a Class Library project for the data access objects and another for the business objects. While developing, you can use Console projects to test the methods on your classes. Fans of extreme programming can build Console projects that can be executed automatically from batch files to unit test that working code has not been broken. On a related note, C# and .
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 27 Chapter 1: .NET Architecture Runtime (CLR). This chapter also discussed the roles of the following features of .NET in the compilation and execution process: ❑ Assemblies and .NET base classes ❑ COM components ❑ JIT compilation ❑ Application domains ❑ Garbage collection Figure 1-4 provides an overview of how these features come into play during compilation and execution. C# Source Code VB.
24727c01.qxd:WroxPro 5/7/07 12:12 PM Page 28 Part I: The C# Language You learned about the characteristics of IL, particularly its strong data typing and object orientation, and how these characteristics influence the languages that target .NET, including C#. You also learned how the strongly typed nature of IL enables language interoperability, as well as CLR services such as garbage collection and security.