WriteLine( ) and Output

The .NET Framework provides a useful method for displaying output on the screen in console applications: System.Console.WriteLine( ). How you use this method will become clearer as you progress through the book, but the fundamentals are straightforward. You call the method, and in the parentheses you pass in a string that you want printed to the console (the screen), as in the Hello World application in Chapter 1.

That’s useful; a string is fixed text, and you might want to output a value that can change, depending on the content of your program. For that, you can also pass in substitution parameters. A substitution parameter is just a placeholder for a value you want to display. For example, you might pass in the substitution parameter {0} as part of the string, and then when you run the program, you’ll substitute the value held in the variable myInt so that its value is displayed where the parameter {0} appears in the WriteLine( ) statement.

Here’s how it works. Each substitution parameter is a number between braces, starting with 0 for the first parameter, 1 for the next, and so on. So, if you’re using just one substitution parameter, it might look like this:

System.Console.WriteLine("Age of student: {0}", myInt);

Notice that you follow the quoted string with a comma and then a variable name. The value of the variable will be substituted into the parameter. If myInt has the value 15, the statement shown previously causes the following to display:

Age of student: 15

If you have more than one parameter, the variable values will be substituted in the order they appear in the method, as in the following:

System.Console.WriteLine("Age of first student: {0},
   age of second student: {1}", myInt, myOtherInt);

If myInt has the value 15 and myOtherInt has the value 20, this will cause the following to display:

Age of first student: 15, and age of second student: 20.

There are other special characters that you can use to format the output, like this:

System.Console.WriteLine("Student ages:
First student:	{0}

                         Second student:	{1}", myInt, myOtherInt);

This produces output that looks like this:

Student ages:
First student:    15
Second student:   20

The characters that begin with a slash character () are called escaped characters. The slash is a signal to the compiler that what follows is an escaped character. The code and the slash together are considered a single character. The escaped character indicates a tab, and the character indicates a newline (a line feed, or a carriage return). The string here will print the characters Student ages: followed by a newline ( ), then the text First student: followed by a tab ( ), then the value of the first parameter ({0}), and a newline character ( ), then the text Second student: followed by a tab ( ), and finally the value of the second parameter ({1}).

You’ll see a great deal more about WriteLine( ) in later chapters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset