AL Part I MA TE RI The C# Language Chapter 2: C# Basics D Chapter 1: .
. NET Architecture 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 .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: 1. 2. The architecture and methodologies of C# reflect the underlying methodologies of .NET.
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).
Chapter 1: .NET Architecture interpreted. One of the disadvantages of Java was that, on execution, the process of translating from Java byte code to native executable resulted in a loss of performance (with the exception of more recent cases, where Java is JIT compiled on certain platforms). Instead of compiling the entire application in one go (which could lead to a slow startup time), the JIT compiler simply compiles each portion of code as it is called (just in time).
Part I: The C# Language you will need to check through the new Visual Basic 2008 code to make sure that the project still works as intended because the conversion might not be perfect. One side effect of this language upgrade is that it is no longer possible to compile Visual Basic 2008 to native executable code. Visual Basic 2008 compiles only to IL, just as C# does. If you need to continue coding in Visual Basic 6, you can do so, but the executable code produced will completely ignore the .
Chapter 1: .NET Architecture A Closer Look at Intermediate Language From what you learned in the previous section, Microsoft Intermediate Language obviously plays a fundamental role in the .NET Framework. As C# developers, we now understand that our C# code will be compiled into IL before it is executed (indeed, the C# compiler compiles only to managed code). It makes sense, then, to now take a closer look at the main characteristics of IL, because any language that targets .
Part I: The C# Language different languages directly communicate with each other, or instantiate instances of each other — it was always 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 need to debug components written in different languages independently.
Chapter 1: .NET Architecture (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 .
Part I: The C# Language Type Reference Type Interface Types Value Type Pointer Types Built-in Value Types Self-describing Types User-defined Value Types Class Types Arrays Enumerations Boxed Value Types Delegates User-defined Reference Types Figure 1-1 The following table explains the types shown in Figure 1-1. 10 Type Meaning Type Base class that represents any type. Value Type Base class that represents any value type.
Chapter 1: .NET Architecture We will not list all of the built-in value types here, because they are covered in detail in Chapter 3, “Objects and Types.” In C#, each predefined type recognized by the compiler maps onto one of the IL built-in types. The same is true in Visual Basic 2008. Common Language Specification The Common Language Specification (CLS) works with the CTS to ensure language interoperability. The CLS is a set of minimum standards that all compilers targeting .NET must support.
Part I: The C# Language component can destroy itself and free up associated memory and resources. The problem with this is that it still relies on the good behavior of clients to notify the component that they have finished with it. It takes only one client not to do so, and the object sits in memory. In some ways, this is a potentially more serious problem than a simple C++-style memory leak because the COM object may exist in its own process, which means that it will never be removed by the system.
Chapter 1: .NET Architecture In pre-.NET days, the choice would be between allowing those instances to share a process (with the resultant risk of a problem in one running instance bringing the whole Web site down) or isolating those instances in separate processes (with the associated performance overhead). Up until now, the only means of isolating code has been through processes. When you start a new application, it runs within the context of a process.
Part I: The C# Language Application domains are designed as a way of separating components without resulting in the performance problems associated with passing data between processes. The idea is that any one process is 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).
Chapter 1: .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.
Part I: The C# Language The fact that an assembly contains program metadata means that applications or other assemblies that call up code in a given assembly do not need to refer to the registry, or to any other data source, to find out how to use that assembly.
Chapter 1: .NET Architecture The solution to these problems is placing shared assemblies in a special directory subtree in the file system, known as the global assembly cache (GAC). Unlike with private assemblies, this cannot be done by simply copying the assembly into the appropriate folder — it needs to be specifically installed into the cache. This process can be performed by a number of .
Part I: The C# Language ensures data types are correctly converted, so the task is no harder than calling the function directly from C++ code would have been — regardless of whether you are coding in C#, C++, or Visual Basic 2008. WinCV, a Windows-based utility, can be used to browse the classes, structs, interfaces, and enums in the base class library. WinCV is discussed in Chapter 15, “Visual Studio 2008.
Chapter 1: .NET Architecture This protects, in most situations, the classes in your application from possible name clashes with classes written by other organizations. Chapter 2, “C# Basics,” looks more closely at namespaces. Creating . NET Applications Using C# C# can also be used to create console applications: text-only applications that run in a DOS window. You will probably use console applications when unit testing class libraries, and for creating UNIX or Linux daemon processes.
Part I: The C# Language Last, but not least, ASP.NET is remarkable for its increased performance. Whereas classic ASP pages are interpreted with each page request, the Web server caches ASP.NET pages after compilation. This means that subsequent requests of an ASP.NET page execute more quickly than the first. ASP.NET also makes it easy to write pages that cause forms to be displayed by the browser, which you might use in an intranet environment.
Chapter 1: .NET Architecture 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 must 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.
Part I: The C# Language code for a basic Windows Service. Chapter 23, “Windows Services,” explores how to write C# Windows Services. Windows Communication Foundation (WCF) Looking at how you move data and services from one point to another using Microsoft-based technologies, you will find that there are a lot of choices at your disposal. For instance, you can use ASP.NET Web services, .NET Remoting, Enterprise Services, and MSMQ for starters.
Chapter 1: .NET Architecture just where to look. Because deploying .NET enterprise components involves simply copying files into directories, developers can now package their classes into more logical, discrete components without encountering “DLL Hell.” Last, but not least, ASP.NET pages coded in C# constitute an excellent medium for user interfaces. Because ASP.NET pages compile, they execute quickly. Because they can be debugged in the Visual Studio 2008 IDE, they are robust.
Part I: The C# Language C# Source Code VB.NET Source Code COMPILATION ASSEMBLY containing IL CODE Language Interoperability through CTS and CLS ASSEMBLY containing IL CODE .