CHAPTER 3 CORE C# PROGRAMMING CONSTRUCTS, PART I
Member
Meaning in Life
BufferHeight
BufferWidth
These properties control the height/width of the console’s buffer area.
Title
This property sets the title of the current console.
WindowHeight
WindowWidth
WindowTop
WindowLeft
These properties control the dimensions of the console in relation to the
established buffer.
Clear()
This method clears the established buffer and console display area.
Basic Input and Output with the Console Class
In addition to the members in Table 3-2, the Console type defines a set of methods to capture input and
output, all of which are static and are, therefore, called by prefixing the name of the class (Console) to the
method name. As you have seen, WriteLine() pumps a text string (including a carriage return) to the
output stream. The Write() method pumps text to the output stream without a carriage return.
ReadLine() allows you to receive information from the input stream up until the Enter key is pressed,
while Read() is used to capture a single character from the input stream.
To illustrate basic I/O using the Console class, create a new Console Application project named
BasicConsoleIO and update your Main() method to call a helper method named GetUserData():
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Basic Console I/O *****");
GetUserData();
Console.ReadLine();
}
}
Note In Chapter 2, you briefly examined Visual Studio code snippets. The cw code snippet is quite useful
during the early chapters of this text, in that it will automatically expand to Console.WriteLine()! To test this for
yourself, type in cw somewhere within your Main() method and hit the Tab key twice. Sadly, there is no code
snippet for Console.ReadLine().
Implement this method within the Program class with logic that prompts the user for some bits of
information and echoes each item to the standard output stream. For example, we could ask the user for
his or her name and age (which we will treat as a text value for simplicity, rather than the expected
numerical value), as follows:
82