Chapter 8: Data Types and Declarations

 

The Six Using Statements

When you begin a new project, that new project is always created with one window (the main window) if you select Windows Application in the New Project window. When the source code file is created for the main window and then displayed, there are six using statements at the top of the file:

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; 

System is a several-megabyte group of over a thousand files. But System alone cannot provide all the machine code your project needs.

System.Collections.Generic contains all the constructs that a project may need (like a definition for file length when a file is named in the source code).

System.ComponentModel contains definitions for all the components that are at the programmer s disposal, like creating a database in the middle of one s source code.

System.Data contains definitions for all data types (like the definition for ASCII in a StreamWriter statement). The principal reason for Data in Visual C# is to support database operations.

System.Drawing contains all the instructions to place controls (like buttons ) on a window template.

System.Windows.Forms contains definitions for all Windows forms, including all the prebuilt windows (like the Open File dialog, for example).

The designers of Visual C# decided that the days of sending large amounts of data to a printer were drawing to a close. They surmised that using data presentation schemes on a screen (written mostly in HTML) and a procedure for routing the screen presentation to a local printer would meet future hard copy needs. Hence, if you want to print from a local program, you must include the statement using System.Drawing.Printing; at the top of the source code file. The other common file that is required is using System.IO (input and output).

This using mechanism is really an invitation to third-party compiler writers who choose to create and market their own collections of classes ” like classes that perform all the bookkeeping functions in a large industrial corporation or classes that account for large amounts of textual material for a publishing company. There are even classes of mathematical functions so NASA and other high-tech engineers can compute trajectories of satellites on a PC.

The largest collection of third-party software in this arena today is computer-aided design software that enables, for example, an architect to create large buildings in a single bound.

Each compiler manufacturer used to distribute wall- size posters that listed all the classes used in the compiler s object-oriented structure. Today, however, due to the proliferation of classes, these posters are no longer used. Also, no one really cares how many classes are present within the confines of a compiler.

All that you care about, as a Visual C# compiler user , is that every executable statement that you enter into source code has a machine code backup list somewhere in the using files. If you type the name of an executable statement into Visual C# and support that statement with a using file that contains all the machine code, then your part of the compilation process is complete.

1.  

If I add using statements (at the top of the source file) that are unnecessary, does this make the compiled project larger than it should be?

Answers

1.  

No, not at all. What the compiler does is this: As it searches for the supporting machine code to support each executable statement, it will invariably enter the using files that are not involved in the necessary executables, and it wastes a little time, meaning a few milliseconds . Once all the useful machine code has been extracted from the read-only files and compilation moves to completion, all the using files are cast out. If your Visual C# compiler did not do this, the simplest project you can write would be several megabytes in size after compilation.

With the advent of OOP, each time a code + local variables packet was needed in the sequence of events in source code, that packet was created in real time with the new statement. For example:

 Timer wxyz = new Timer(); wxyz.Interval = 1000; 

When the first statement above is executed, the new instance of the Timer class is created ( instantiated ) by a constructor. Constructors would have been easy to write (by the compiler writers) except that the new languages such as Visual C# allowed multiple forms for classes like Timer. For lack of a better name, compiler writers called the simplest form of the class the basic form and called all the other forms overloaded forms.

Modern OOP compilers create (instantiate) just about every packet of code that appears in a project, but the compiler doesn t force the programmer to do the repetitive instantiation. Instead, most of the recreating of these packets is done automatically and no human is involved. For example, every time an ANSIString is operated upon (to add or delete a character, change a character, or concatenate several strings into one string), the entire basic string is created new with every operation. All the old occurrences of the code are readied for the garbage collector, which clears the unused memory and makes it available to the operating system again.

 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

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