Sams Teach Yourself C in 21 Days

(singke) #1
The C# Programming Language 775

BD7


As you can see, the difference between the two routines is that WriteLine()automati-
cally goes to a new line after the text is displayed, whereas Write()does not. Listing
B7.3 shows the two routines in action.

LISTINGB7.3 display.cs—Using WriteLine()andWrite()
1: // display.cs - printing with WriteLine and Write
2: //———————————————————————-
3:
4: class display
5: {
6: public static void Main()
7: {
8: System.Console.WriteLine(“First WriteLine Line”);
9: System.Console.WriteLine(“Second WriteLine Line”);
10:
11: System.Console.Write(“First Write Line”);
12: System.Console.Write(“Second Write Line”);
13:
14: // Passing parameters
15: System.Console.WriteLine(“\nWriteLine: Parameter = {0}”, 123 );
16:
17: System.Console.Write(“Write: Parameters = {0} and {1}”, 456, 789);
18: }
19: }

First WriteLine Line
Second WriteLine Line
First Write LineSecond Write Line
WriteLine: Parameter = 123
Write: Parameters = 456 and 789
This listing uses the System.Console.WriteLine()routine on lines 8 and 9 to
print two pieces of text. You can see from the output that each of these print on a
separate line. Lines 11 and 12 show the System.Console.Write()routine. These two
lines print on the same line. There is not a return line feed after printing. Lines 15 and 17
show each of these routines with the use of a parameter. You should notice that the para-
meters are not implemented the same as in C. With C and printf()you used a percent-
age sign to indicate a parameter. In C# you include a parameter value between braces.
You should also notice that the parameters in C# are numbered. Parameters are not sim-
ply applied in order like in C and printf(). Rather, the first parameter is applied to {0},
the second to {1}.

OUTPUT

ANALYSIS

42 448201x-Bonus7 8/13/02 11:24 AM Page 775

Free download pdf