PART I RI MA CHAPTER 2: Core C# TE CHAPTER 1: .
1 .NET Architecture WHAT’ S IN THIS CHAPTER? ➤ Compiling and running code that targets .NET ➤ Advantages of Microsoft Intermediate Language (MSIL) ➤ Value and reference types ➤ Data typing ➤ Understanding error handling and attributes ➤ Assemblies, .NET base classes, and namespaces Throughout this book, we emphasize that the C# language must be considered in parallel with the .NET Framework, rather than viewed in isolation. The C# compiler specifically targets .
❘ CHAPTER 1 .NET ARCHITECTURE One important thing to make clear is that C# is a language in its own right. Although it is designed to generate code that targets the .NET environment, it is not itself part of .NET. Some features are supported by .NET but not by C#, and you might be surprised to learn that some features of the C# language are not supported by .NET (for example, some instances of operator overloading)! However, because the C# language is intended for use with .
The Common Language Runtime ❘5 This explains why we can expect that execution of managed IL code will be almost as fast as executing native machine code. What it does not explain is why Microsoft expects that we will get a performance improvement. The reason given for this is that, because the fi nal stage of compilation takes place at runtime, the JIT compiler will know exactly what processor type the program will run on.
❘ CHAPTER 1 .NET ARCHITECTURE that it will run independently of the .NET runtime. If you want your C++ code to run within the .NET Framework, you can simply add the following line to the beginning of your code: #using You can also pass the flag /clr to the compiler, which then assumes that you want to compile to managed code, and will hence emit IL instead of native machine code.
A Closer Look at Intermediate Language ❘7 Support for Object Orientation and Interfaces The language independence of .NET does have some practical limitations. IL is inevitably going to implement some particular programming methodology, which means that languages targeting it need to be compatible with that methodology. The particular route that Microsoft has chosen to follow for IL is that of classic object- oriented programming, with single implementation inheritance of classes.
❘ CHAPTER 1 .NET ARCHITECTURE This is all quite an ambitious aim, but amazingly, .NET and IL have achieved it. In the case of stepping between methods in the debugger, this facility is really offered by the Visual Studio integrated development environment (IDE) rather than by the CLR itself. Distinct Value and Reference Types As with any programming language, IL provides a number of predefi ned primitive data types.
A Closer Look at Intermediate Language ❘9 Suppose that one of the methods of a Visual Basic 2010 class is defi ned to return an Integer — one of the standard data types available in Visual Basic 2010. C# simply does not have any data type of that name. Clearly, you will be able to derive from the class, use this method, and use the return type from C# code, only if the compiler knows how to map Visual Basic 2010s Integer type to some known type that is defi ned in C#.
❘ CHAPTER 1 .NET ARCHITECTURE This example shows that the CLS works in two ways. 1. Individual compilers do not have to be powerful enough to support the full features of .NET — this should encourage the development of compilers for other programming languages that target .NET. 2. If you restrict your classes to exposing only CLS - compliant features, then it guarantees that code written in any other compliant language can use your classes.
A Closer Look at Intermediate Language ❘ 11 ones are accessible from your code — that is, which objects have references that refer to them. Any 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.
❘ CHAPTER 1 .NET ARCHITECTURE 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. This provides an excellent guarantee that any badly behaved code will not be able to damage anything outside of its own address space.
A Closer Look at Intermediate Language ❘ 13 out- of-bounds array operations are permitted. If a running application does need to communicate or share data with other applications running in different application domains, it must do so by calling on .NET ’s remoting services. Code that has been verified to check that it cannot access data outside its application domain (other than through the explicit remoting mechanism) is said to be memory type safe.
❘ CHAPTER 1 .NET ARCHITECTURE ASSEMBLIES An assembly is the logical unit that contains compiled code targeted at the .NET Framework. Assemblies are not covered in detail in this chapter because they are covered thoroughly in Chapter 18, “Assemblies,” but we summarize the main points here.
Assemblies ❘ 15 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 software that is designed specifically to perform malicious damage). There are also no problems with name collisions.
❘ CHAPTER 1 .NET ARCHITECTURE .NET FRAMEWORK 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 that IL uses, based on single inheritance.
Creating .NET Applications Using C# ❘ 17 NAMESPACES Namespaces are the way that .NET avoids name clashes between classes. They are designed to prevent situations in which you defi ne 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 .NET ARCHITECTURE ASP days.) Because you can factor a page’s functionality into event handlers with explicit meanings, ASP. NET pages are easier to understand. Another nice thing about ASP.NET pages is that you can create them in Visual Studio 2010, the same environment in which you create the business logic and data access components that those ASP.NET pages use. A Visual Studio 2010 project, or solution, contains all the fi les associated with an application.
Creating .NET Applications Using C# ❘ 19 and proprietary formats such as Electronic Data Interchange (EDI). XML Web services are designed for a service- oriented Web, in which remote computers provide each other with dynamic information that can be analyzed and reformatted, before fi nal presentation to a user. An XML Web service is an easy way for a computer to expose information to other computers on the Web in the form of XML. In technical terms, an XML Web service on .NET is an ASP.
❘ CHAPTER 1 .NET ARCHITECTURE Windows Services A Windows Service (originally called an NT Service) is a program designed to run in the background in Windows NT/2000/XP/2003/Vista/7 (but not Windows 9x). Services are useful when you want a program to be running continuously and ready to respond to events without having been explicitly started by the user. A good example is the World Wide Web Service on web servers, which listens for web requests from clients. It is very easy to write services in C#. .
Summary ❘ 21 to focus on gluing their data access objects together in methods that accurately enforce their organizations’ business rules. Moreover, with attributes, C# business objects can be outfitted for method-level security checks, object pooling, and JIT activation supplied by COM+ Services. Furthermore, .NET ships with utility programs that allow your new .NET business objects to interface with legacy COM components.
❘ CHAPTER 1 .NET ARCHITECTURE ➤ JIT compilation ➤ Application domains ➤ Garbage collection Figure 1- 4 provides an overview of how these features come into play during compilation and execution. VB.NET Source Code C# Source Code COMPILATION ASSEMBLY containing IL CODE Language Interoperability through CTS and CLS ASSEMBLY containing IL CODE .