User:Foxall/24

Ka Wiktionary

./ ADD NAME=CH24.HTM

[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch23lev1sec8.htm Previous Section] [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch24lev1sec1.htm Next Section]

Hour 24. The 10,000-Foot View

You know a lot about C# by now. You can create projects, use forms and controls to build an interface, and you know how to add menus and toolbars to a form. You've also learned how to create modules and procedures and how to write code to make things happen. You can use variables, make decisions, perform looping, and even debug your code. The question you may be thinking now is, "Where to next?"

Throughout this book, I've focused my discussions on C#. When it comes to Microsoft's .NET platform, however, C# is just part of the picture. In this hour, I provide an overview of Microsoft's .NET platform so that you can see how C# relates to .NET as a whole. By the time you finish this hour, you'll understand the various pieces of .NET and how they are interrelated. Hopefully, you'll be able to combine this information with your current personal and professional needs to determine the facets of .NET that you want to explore in more detail.

The highlights of this hour include the following:

  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch24lev1sec1.htm#ch24lev1sec1 Introduction to the .NET Framework]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch24lev1sec2.htm#ch24lev1sec2 Appreciating the Common Language Runtime (CLR)]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch24lev1sec3.htm#ch24lev1sec3 Understanding Microsoft Intermediate Language (IL)]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch24lev1sec4.htm#ch24lev1sec4 Working with namespaces]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch24lev1sec5.htm#ch24lev1sec5 The Common Type System]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch24lev1sec6.htm#ch24lev1sec6 Garbage collection]

The .NET Framework

The components and technology that make up Microsoft .NET are collectively called the .NET Framework. The framework comprises numerous classes and includes components such as the Common Language Runtime, Microsoft Intermediate Language, and ADO.NET. In the following sections, I'll explain the various pieces that make up the .NET Framework.

Common Language Runtime (CLR) 

A language runtime is what allows an application to run on a target computer; it consists of code that is shared among all applications developed using a supported language. A runtime contains the "guts" of language code, such as code that draws forms to the screen, handles user input, and manages data. The runtime of .NET is called the Common Language Runtime (CLR).

Unlike runtimes for other languages, the CLR is designed as a multilanguage runtime. For example, C# and Visual Basic both use the CLR. In fact, currently more than 15 language compilers are being developed to use the CLR.

Because all .NET languages share the CLR, they share the same IDE, the same forms engine, the same exception-handling mechanism, the same garbage collector (discussed shortly), and much more. One benefit of the multilanguage capability of the CLR is that programmers can leverage their knowledge of a given .NET language. For example, some developers on a team may be comfortable with C#, whereas others are more comfortable with Visual Basic. Because both languages share the same runtime, both can be integrated to deliver a solution. In addition, a common exception-handling mechanism is built into the CLR so that exceptions can be thrown from code written in one language and caught in code written in another .NET language.

graphics/newterm.gif Code that runs within the CLR is called managed code because the code and resources used by the code (variables, objects, and so on) are fully managed by the CLR. C# is restricted to working only in managed code, but some languages (such as C++) are capable of dropping to unmanaged code�code that isn't managed by the CLR. One big advantage with working in managed code is that the CLR provides garbage collection�the automatic freeing up of unused resources. You'll learn a bit more about garbage collection later in this hour.

Another advantage of the CLR is that all .NET tools share the same debugging and code-profiling tools. In the past, languages such as Visual Basic were limited to their own debugging tools, whereas languages such as C++ had many third-party debugging tools available. Now, all languages share the same tools. This means that as advancements are made to the debugging tools of one product, they're made to tools of all products because the tools are shared. This aspect goes beyond debugging tools. For example, add-ins to the IDE (such as code managers) are just as readily available to C# as they are to Visual Basic�or any other .NET language, for that matter.

graphics/bookpencil.gif Although Microsoft hasn't announced any official plans to do so, it's possible that Microsoft could produce a version of the CLR that runs on other operating systems, such as Macintosh or Linux. If this occurs, the applications that you've written for Windows should run on a newly supported operating system with little or no modification.

Microsoft Intermediate Language

As you can see in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/24.htm#ch24fig01 Figure 24.1], all .NET code, regardless of the language syntax used, compiles to Intermediate Language (IL) code. IL code is the only code the CLR understands; it doesn't understand C#, Visual Basic, or any other developer syntax. It's IL that gives .NET its multilanguage capabilities; as long as an original source language can be compiled to IL, it can become a .NET language. For example, people are developing a .NET compiler for COBOL�a mainframe language with a long history. This compiler will take existing COBOL code and compile it to IL so that it will run within the .NET Framework using the CLR. COBOL itself isn't a Windows language and doesn't support many of the features found in a true Windows language (such as a Windows Forms engine), so you can imagine the excitement of COBOL programmers at the prospect of being able to leverage their existing code and programming skills to create powerful Windows applications.

Figure 24.1. These are the steps taken to turn developer code into a running component.

graphics/24fig01.gof

graphics/bookpencil.gif One of the potential drawbacks of IL is that it may be susceptible to reverse compilation. This has many people questioning the security of .NET code and the security of the .NET Framework, in general. If code security is a serious concern for you, I encourage you to research this matter on your own.
graphics/newterm.gif IL code isn't the final step in the process of compiling and running an application. For a processor (CPU) to execute programmed instructions, those instructions must be in machine-language format. When you run a .NET application, a just-in-time compiler (called a JITter) compiles the IL to machine-language instructions that the processor can understand. IL code is processor independent, which again brings up the possibility that JITters could be built to create machine code for computers that are using something other than Intel-compatible processors. If Microsoft were to offer a CLR for operating systems other than Windows, much of the differences would lie in the way IL would be compiled by the JITter.

As .NET evolves, changes made to the CLR will benefit all .NET applications. For example, if Microsoft finds a way to further increase the speed at which forms are drawn to the screen by making improvements to the CLR, all .NET applications will immediately benefit from the improvement. Optimizations made to a specific syntax compiler, such as the one that compiles C# code to IL, are language specific, however. This means that even though all .NET languages compile to IL code and use the CLR, it's possible for one language to have small advantages over another because of the way in which the language's code is compiled to IL.

>

Namespaces

graphics/newterm.gif As I mentioned earlier, the .NET Framework is composed of classes�many classes. Namespaces are the method used to create a hierarchical structure of all these classes and they help prevent naming collisions. A naming collision occurs when two classes have the same name. Because namespaces provide a hierarchy, it's possible to have two classes with the same name, as long as they exist in different namespaces. Namespaces, in effect, create a scope for classes.

The base namespace in the .NET Framework is the System namespace. The System namespace contains classes for garbage collection (discussed shortly), exception handling, data typing, and so much more. The System namespace is just the tip of the iceberg. There are literally dozens of namespaces. [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/24.htm#ch24table01 Table 24.1] lists some of the more common namespaces, many of which you've used in this book. All the controls that you've placed on forms and even the forms themselves, for example, belong to the System.Windows.Forms namespace. Use [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/24.htm#ch24table01 Table 24.1] as a guide; if a certain namespace interests you, I suggest that you research it further in the Visual Studio .NET online help.

Namespace Description
Microsoft.CSharp Contains classes that support compilation and code generation using the C# language.
Microsoft.VisualBasic Contains classes that support compilation and code generation using the Visual Basic language.
System Contains fundamental classes and base classes that define commonly used value and reference data types, event handlers, interfaces, attributes, and exceptions. This is the base namespace of .NET.
System.Data Contains classes that constitute the ADO.NET architecture.
System.Diagnostics Contains classes that enable you to debug your application and to trace the execution of your code.
System.Drawing Contains classes that provide access to the Graphical Device Interface (GDI) basic graphics functionality.
System.IO Contains classes that allow reading from and writing to data streams and files.
System.Net Contains classes that provide a simple programming interface to many of the protocols found on the network.
System.Security Contains classes that provide the underlying structure of the CLR security system.
System.Web Contains classes that provide interfaces that enable browser/server communication.
System.Windows.Forms Contains classes for creating Windows-based applications that take advantage of the rich user-interface features available in the Microsoft Windows operating system.
System.Xml Contains classes that provide standards-based support for processing XML.
graphics/bookpencil.gif All Microsoft-provided namespaces begin with either System or Microsoft. Other vendors can provide their own namespaces, and it's possible for you to create your own custom namespaces as well, but that's beyond the scope of this book.

Common Type System

The Common Type System in the CLR is the component that defines how data types are declared and used. The fact that the CLR can support cross-language integration to the level it does is largely because of the Common Type System. In the past, each language used its own data types and managed data in its own way. This made it very difficult for applications developed in different languages to communicate, because no standard way existed in which to pass data between them.

The Common Type System ensures that all .NET applications use the same data types, provides for self-describing type information (called metadata), and controls all the data manipulation mechanisms so that data is handled (stored and processed) in the same way among all .NET applications. This allows data (including objects) to be treated the same way in all .NET languages.

Garbage Collection[wax ka badal]

Although I've talked a lot about objects (you can't talk about anything .NET related without talking about objects), I've avoided discussing the underlying technical details of how .NET creates, manages, and destroys objects. Although you don't need to know the complex minutiae of how .NET works with objects, you do need to understand a few details of how objects are destroyed.

graphics/newterm.gif As I discussed in previous hours, setting an object variable to null or letting it go out of scope destroys the object. However, as I mentioned in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch17.htm#ch17 Hour 17], "Designing Objects Using Classes," this isn't the whole story. The .NET platform uses a garbage collector for destroying objects. The specific type of garbage collection implemented by .NET is called reference-tracing garbage collection. Essentially, the garbage collector monitors the resources used by a program, and when resources reach a defined threshold, the garbage collector proceeds to look for unused objects. When the garbage collector finds an unused object, it destroys it, freeing all the memory and resources the object was using.

An important thing to remember about garbage collection is that releasing an object by setting it to null or letting an object variable go out of scope doesn't mean the object will be destroyed immediately. The object won't be destroyed until the garbage collector is triggered to go looking for unused objects.

Summary 

Now that you've completed this book, you should have a solid working understanding of developing applications with C#. Nevertheless, you've just embarked on your journey. One of the things I love about developing applications is that there is always something more to learn, and there's always a better approach to a development problem. In this hour, I acquainted you with the "bigger picture" of Microsoft's .NET platform by exposing you to the .NET Framework and its various components. Consider the information you learned in this hour a primer; what you do with this information and where you go from here is entirely up to you.

I wish you the best of luck on your programming endeavors!

>

Q&A

[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/24.htm#qad1e69464 Q1:] The .NET Framework seems incredibly complex and daunting. How should I go about learning it?
A1: I wouldn't recommend that you attempt to learn the entire framework; I have no plans to do so, either. Instead, focus on areas of interest or research facets of the framework as the need arises. For instance, if security is very important to you, learn about System.Security. Attempting to learn everything you can about the framework may not be as productive as spending your time mastering user-interface design and general programming skills.
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/24.htm#qad1e69474 Q2:] Does an end user have to have the Common Language Runtime on his or her computer to run my .NET application?
A2: Yes. In [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch22.htm#ch22 Hour 22], "Deploying a Solution," I discussed how to deploy a solution, as well as how to include the Common Language Runtime. If you know for a fact that your end user has the runtime, you can remove it from your setup, vastly reducing the size of your setup program.

Workshop

The Workshop is designed to help you anticipate possible questions, review what you've learned, and get you thinking about how to put your knowledge into practice. The answers to the quiz are in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01.htm#app01 Appendix A], "Answers to Quizzes/Exercises."

 Quiz
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec24.htm#ch24ans01 1:] The classes and technology that make up .NET's underlying infrastructure are called what?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec24.htm#ch24ans02 2:] What is the name of the shared .NET runtime?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec24.htm#ch24ans03 3:] True or False: Each .NET language uses its own code editor and debugging tools.
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec24.htm#ch24ans04 4:] True or False: Code that runs within the Common Language Runtime is called unmanaged code.
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec24.htm#ch24ans05 5:] Code written in a .NET language such as C# is compiled to what?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec24.htm#ch24ans06 6:] What are namespaces?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec24.htm#ch24ans07 7:] One of the things that gives .NET its multilanguage capabilities is that all data is declared and managed in the same way across all languages. This is handled by .NET's what?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec24.htm#ch24ans08 8:] What destroys objects, freeing all the resources that they consume?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/24.htm#toppage Top]