.NET Framework Classes

 
Chapter 1 - C# and .NET Architecture
bySimon Robinsonet al.
Wrox Press 2002
  

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 have been written by Microsoft, and which allow you to do almost any of the tasks that were previously available through the Windows API. These classes follow the same object model as used by IL, based on single inheritance. This means that you can either instantiate objects of whichever .NET base class is appropriate, or you can derive your own classes from them.

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 . This approach will be familiar to Visual Basic and Java developers, whose respective libraries are just as easy to use. It may however come as a great relief to C++ developers, who for years have had to cope with such API functions as GetDIBits() , RegisterWndClassEx() , and IsEqualIID() , as well as a whole plethora of functions that required Windows handles to be passed around.

On the other hand, C++ developers always had easy access to the entire Windows API, whereas Visual Basic and Java developers were more restricted in terms of the basic operating system functionality that they have access to from their respective languages. What is new about the .NET base classes is that they combine the ease of use that was typical of the Visual Basic and Java libraries with the relatively comprehensive coverage of the Windows API functions. There are still many features of Windows that are not available through the base classes, and for which you will need to call into the API functions, but in general, these are now confined to the more exotic features. For everyday use, you will probably find the base classes adequate. And if you do need to call into an API function, .NET offers a so-called "platform-invoke" which will ensure data types are correctly converted, so the task is no harder than calling the function directly from C++ code would have been, no matter whether you are coding in C#, C++, or VB.NET.

WinCV, a Windows-based utility, can be used to browse the classes, structs, interfaces, and enums in the base class library. We'll examine WinCV in Chapter 6.

Although Chapter 5 is nominally dedicated to the subject of base classes, in reality, once we have completed our coverage of the syntax of the C# language, most of the rest of this book will essentially be showing you how to use various classes within the .NET base class library. That is how comprehensive base classes are. As a rough guide, the areas covered by the .NET base classes include:

  • Core features provided by IL (including, say, the primitive data types in the CTS, Chapter 5)

  • Windows GUI support and controls (Chapter 7)

  • Web Forms (ASP.NET, 16)

  • Data Access (ADO.NET, 10)

  • Directory Access (Chapter 13)

  • File system and registry access (Chapter 12)

  • Networking and web browsing (Chapter 20)

  • .NET attributes and reflection (Chapter 5)

  • Access to aspects of the Windows OS (environment variables and so on, see Chapter 23)

  • COM interoperability (18)

Incidentally, according to Microsoft sources, a large proportion of the .NET base classes have actually been written in C#!

Namespaces

Namespaces are the way that .NET avoids name clashes between classes. They are designed, for example, 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 (quite a likely scenario the proportion of businesses that have customers seems to be quite high).

A namespace is no more than a grouping of data types, but it has the effect that the names of all data types within a namespace automatically get prefixed with the name of the namespace. It is also possible to nest namespaces within each other. For example, most of the general-purpose .NET base classes are in a namespace called System . The base class Array is in this namespace, so its full name is System.Array .

.NET requires all types to be defined in a namespace, so for example you could place your Customer class in a namespace called YourCompanyName . This class would have the full name YourCompanyName.Customer .

If a namespace is not explicitly supplied, then the type will be added to a nameless global namespace.

Microsoft recommends that for most purposes you supply at least two nested namespace names, the first one being the name of your company, the second being the name of the technology or software package that the class is a member of, such as YourCompanyName.SalesServices.Customer . Doing this will, in most situations, protect the classes in your application from possible name clashes with classes written by other organizations.

We will look more closely at namespaces in Chapter 2.

  


Professional C#. 2nd Edition
Performance Consulting: A Practical Guide for HR and Learning Professionals
ISBN: 1576754359
EAN: 2147483647
Year: 2002
Pages: 244

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net