Declaring a Number as a Constant

 

Overloading

The best example of multiple forms for a single class is the StreamWriter class, a class that is used to write data to a file. In addition to the basic format, there are at least seven overloaded formats. (Note that the term Stream below means the file that is to be written to.)

  1. public StreamWriter(Stream); This is the basic format. Characters are written from a predetermined buffer with a fixed number of chars, in a predetermined character format (like ASCII), to the Stream.

  2. public StreamWriter(string); This form writes the data from a predetermined buffer in a predetermined format to a named ANSIString.

  3. public StreamWriter(Stream, Encoding); This format is the same as #1, but the programmer can name an encoding format other than ASCII.

  4. public StreamWriter(string, bool); This format is the same as #2 above, but it includes a switch that allows the writing to begin at the beginning of the string or at the end of the string (if any characters are already in the string). This is known as the append mode.

  5. public StreamWriter(Stream, Encoding, int); This format is the same as #3 above, but it includes a third argument, Int, which is the number of chars to be written to the Stream.

  6. public StreamWriter(string, bool, Encoding); This format is the same as #4 above, but it includes a third argument, Encoding, which specifies the format in which to write the data (other than the default ASCII format).

  7. public StreamWriter(string, bool, Encoding, int); This format is the same as #6 above, but it includes a fourth argument, Int, which is the number of chars to be written to the Stream.

  8. public StreamWriter(Stream, bool, Encoding, int); This format is the same as #5 above, but it includes a fourth argument to decide if the new block of data should be entered at the start of the Stream (overwriting all other data) or appended to the end of existing data.

In formats #3 and #5 the compiler will accept an alternate form:

 public StreamWriter(Stream, ,Encoding, int); 

In this case the blank between the first argument (Stream) and the second argument (Encoding) indicates a blank entry, which is unused here ” a bool variable.

1.  

Why didn t the compiler writers just create eight forms of StreamWriter, named StreamWriter1 through StreamWriter8, and let it go at that?

image from book

2.  

How come the programmer did not have to create an instance of StreamWriter (using new) before the StreamWriter statement was used in the source code?

image from book

Answers

1.  

This Visual C# compiler is burdened with up to 800 MB worth of machine code in its library, and anything that could be done to diminish the size of the machine code package was welcome. By allowing this overloading, Microsoft was able to create one form of StreamWriter and set the branching points within this machine code to accommodate all the variations in StreamWriter that a programmer would ever need.

2.  

The most common Visual C# statements are equipped with automatic constructors that perform the instantiation of their object(s) each time they are used. The less common objects (and Timer above is a good example of a little-used object) must be manually instantiated by the programmer.

 


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