Sams Teach Yourself C in 21 Days

(singke) #1
Displaying Basic Information

It is beyond the scope of a single day’s lessons to teach you a lot about C#. To give you a
small look at C#, two methods will be presented. These are methods for displaying infor-
mation. Two routines for displaying basic information are


  • System.Console.WriteLine()

  • System.Console.Write()
    These routines print information to the screen. Both print information in the same man-
    ner, with only one small difference. The WriteLine()routine writes information to a
    new line. This is similar to the puts()function in C. The Write()routine does not start
    a new line when information is written. This is similar to the printf()function in C.
    The information you want displayed on the screen is written between the parentheses. If
    you are printing text, you include the text between the parentheses and within double
    quotes. For example, the following prints the text “Hello World“:
    System.Console.WriteLine(“Hello World”);
    This prints Hello Worldon the screen. The following examples illustrate other text
    being printed:
    System.Console.WriteLine(“This is a line of text”);
    System.Console.WriteLine(“This is a second line of text”);
    If you execute these consecutively, you see the following displayed:
    This is a line of text
    This is a second line of text
    Now consider the following two lines. If these execute consecutively, what do you see
    printed?
    System.Console.WriteLine(“Hello “);
    System.Console.WriteLine(“World!”);
    If you guessed that these would print
    Hello World!
    you are not correct! Those lines print the following:
    Hello
    World!
    Notice that each word is on a separate line. If you execute the two lines using the
    Write()routine instead, you get the results you want:
    Hello World!


774 Bonus Day 7

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

Free download pdf