Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 1 C# AND THE .NET FRAMEWORK

Before .NET

The C# programming language was designed for developing programs for Microsoft’s .NET Framework. This chapter gives a brief look at where .NET came from and its basic architecture. To start off, let’s get the name right: C# is pronounced “see sharp.”1

Windows Programming in the Late 1990s

In the late 1990s, Windows programming using the Microsoft platform had fractured into a number of branches. Most programmers were using Visual Basic (VB), C, or C++. Some C and C++ programmers were using the raw Win32 API, but most were using the Microsoft Foundation Classes (MFC). Others had moved to the Component Object Model (COM).

All these technologies had their own problems. The raw Win32 API was not object-oriented, and using it required a lot more work than MFC. MFC was object-oriented but was inconsistent and getting old. COM, although conceptually simple, was complex in its actual coding and required lots of ugly, inelegant plumbing.

Another shortcoming of all these programming technologies was that they were aimed primarily at developing code for the desktop rather than the Internet. At the time, programming for the Web was an afterthought and seemed very different from coding for the desktop.

Goals for the Next-Generation Platform Services

What we really needed was a new start—an integrated, object-oriented development framework that would bring consistency and elegance back to programming. To meet this need, Microsoft set out to develop a code execution environment and a code development environment that met these goals.

Figure 1-1 lists these goals.

Figure 1-1. Goals for the next-generation platform

Enter Microsoft .NET

In 2002, Microsoft released the first version of the .NET Framework, which promised to address the old problems and meet the goals for the next-generation systems. The .NET Framework is a much more consistent and object-oriented environment than either the MFC or COM programming technology. Some of its features include the following:

1 I was once interviewed for a contract C# position when the Human Resources interviewer asked me how much experience I’d had programming in “see pound” (instead of “see sharp”)! It took me a moment to realize what he was talking about.

2

CHAPTER 1 C# AND THE .NET FRAMEWORK

Multiple platforms: The system runs on a broad range of computers, from servers and desktop machines to PDAs and cell phones.

Industry standards: The system uses industry-standard communication protocols, such as XML, HTTP, SOAP, and WSDL.

Security: The system can provide a much safer execution environment, even in the presence of code obtained from suspect sources.

Components of the .NET Framework

The .NET Framework consists of three components, as shown in Figure 1-2. The execution environment is called the Common Language Runtime (CLR). The CLR manages program execution at run time, including the following:

Memory management

Code safety verification

Code execution, thread management, and exception handling

Garbage collection

The programming tools include everything you need for coding and debugging, including the following:

The Visual Studio integrated development environment

.NET-compliant compilers (e.g., C#, VB .NET, JScript, F#, IronRuby, and managed C++)

Debuggers

Web development server-side technologies, such as ASP.NET or WCF

The Base Class Library (BCL) is a large class library used by the .NET Framework and available for you to use in your programs as well.

Figure 1-2. Components of the .NET Framework

3

CHAPTER 1 C# AND THE .NET FRAMEWORK

An Improved Programming Environment

The .NET Framework offers programmers considerable improvements over previous Windows programming environments. The following sections give a brief overview of its features and their benefits.

Object-Oriented Development Environment

The CLR, the BCL, and C# are designed to be thoroughly object-oriented and act as a well-integrated environment.

The system provides a consistent, object-oriented model of programming for both local programs and distributed systems. It also provides a software development interface for desktop application programming, mobile application programming, and web development, consistent across a broad range of targets, from servers to cell phones.

Automatic Garbage Collection

The CLR has a service called the garbage collector (GC), which automatically manages memory for you.

The GC automatically removes objects from memory that your program will no longer access.

The GC relieves programmers of tasks they have traditionally had to perform, such as deallocating memory and hunting for memory leaks. This is a huge improvement, since hunting for memory leaks can be difficult and time-consuming.

Interoperability

The .NET Framework was designed for interoperability between different .NET languages, the operating system or Win32 DLLs, and COM.

.NET language interoperability allows software modules written using different .NET languages to interact seamlessly.

A program written in one .NET language can use and even inherit from a class written in another .NET language, as long as certain rules are followed.

Because of its ability to easily integrate modules produced in different programming languages, the .NET Framework is sometimes described as language-agnostic.

4

CHAPTER 1 C# AND THE .NET FRAMEWORK

.NET provides a feature called platform invoke (P/Invoke), which allows code written for .NET to call and use code not written for .NET. It can use raw C functions imported from standard Win32 DLLs, such as the Windows APIs.

The .NET Framework also allows interoperability with COM. The .NET Framework software components can call COM components and COM components can call .NET components as if they were COM components themselves.

No COM Required

The .NET Framework frees the programmer from the COM legacy. As a C# programmer, you don’t need to use COM and therefore don’t need any of the following:

The IUnknown interface: In COM, all objects must implement interface IUnknown. In contrast, all

.NET objects derive from a single class called object. Interface programming is still an important part of .NET, but it’s no longer the central theme.

Type libraries: In COM, type information is kept in type libraries as .tlb files, which are separate from the executable code. In .NET, a program’s type information is kept bundled with the code in the program file.

Reference counting: In COM, the programmer had to keep track of the number of references to an object to make sure it wasn’t deleted at the wrong time. In .NET, the GC keeps track of references and removes objects only when appropriate.

HRESULT: COM used the HRESULT data type to return runtime error codes. .NET doesn’t use HRESULTs. Instead, all unexpected runtime errors produce exceptions.

The registry: COM applications had to be registered in the system registry, which holds information about the configurations of the operating system and applications. .NET applications don’t need to use the registry. This simplifies the installation and removal of programs. (However, there is something similar called the global assembly cache, which I’ll cover in Chapter 10.)

Although the amount of COM code that’s currently being written is fairly small, there’s still quite a number of COM components in systems currently being used, and C# programmers sometimes need to write code that interfaces with those components. C# 4.0 introduces several new features that make that task easier.

5

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]