The SLAR on System.Console
More from the series sharing some of the information in the .NET Framework Standard Library Annotated Reference Vol 1 here is an annotation from the System.Console attribute.
Notice the heavy use of method overloading in this class. We special-case each
of the primitive types in order to avoid boxing overhead. We could have gotten away
with just having the overload that takes the object, but then calls such as these would
cause an extra allocation for the coercion to object:
Console.WriteLine (42);
and
Console.WriteLine ('c');
In general this kind of special casing is not necessary, but for a class that is likely to be
used in a tight loop it can be very helpful.
This class is a classic example of “design in reverse.” Before we designed this class we
knew what we wanted the Hello World example to look like:
Console.WriteLine ("Hello World");
We then created this class to make that sample code a reality. Even in the face of the
full complexity of a well-factored I/O system, we kept the simple things simple.
It is interesting to note that all of the Console.WriteXxx() methods are just
shorthand for Console.Out.WriteXxx(). Likewise, Console.ReadXxx() is
shorthand for Console.In.ReadXxx(). This design enables the In and Out
streams to be available directly in the rare cases where they are needed but does not
complicate the simple usage with them.
Brian Grunkemeyer
The OpenStandardXxx methods that take an integer for the buffer size are
really rather useless. We eventually decided that supporting buffering on the streams
used for Console is a silly notion, especially since classes like StreamReader and
StreamWriter may internally do their own buffering.
Kit George
Note that there is no easy way for a user to get a single key press: Return has to
be pressed before Read or ReadLine complete. This did not seem like a critical scenario
when we designed this class originally, but it is interesting to note that this has
been one of our top requests for the Console class since Version 1 was released.