Examples
First, this program is divided into two parts: the Test1() and Test2() subs. In the Test1() subroutine, we show how you can use the Console.WriteLine sub with an integer, a string literal, a Char array, a format pattern string, and with no arguments. In the Test2() subroutine, we use the Console.Write sub in the same general ways.
Console.WriteLine versus Console.Write. The Console.WriteLine is different from Console.Write because it always appends a newline sequence to each part it writes. You never need to add newlines manually to Console.WriteLine unless you want extra blank lines. Console.Write does not add anything after what you write; you can call Console.Write and Console.WriteLine together to write partial lines and then finish them.
Writing Char arrays. The two array() variables in the program are written to the console with Console.WriteLine and Write. You can specify a range of the array to write, or write the entire array; both examples are demonstrated above.
Format strings. The example shows how you can use format strings with Console.WriteLine and Console.Write. The substitution markers {0} and {1} are replaced in the output with the following arguments in the corresponding order. This is a very clear way to form complex text outputs.
Writing objects
You can pass any object to the Console.WriteLine and Console.Write subroutines. This is because one overload of these methods receives the Object type; when received, the subroutine will internally call the ToString method on the Object instance, resulting in a text representation.
No comments:
Post a Comment