5.2. Basic console I/O

5.2.1. Writing to console

For non-GUI Java programs, you use System.out.println() to output something to the command line console. For C#, you use System.Console.WriteLine(). WriteLine is a static method of the Console class, which is found in the System namespace (see section 5.3). Both methods are similar – you can pass in a string, a numeric value, or a combination of strings and numeric values (using the + concatenation operator).

This example should make things clear.

 1: using System;
 2:
 3: class TestClass{
 4:   public static void Main(){
 5:     Console.WriteLine
 6:         ("Words\n" + "and " + 99 + " numbers.");
 7:     Console.WriteLine
 8:         ("Interesting {0} of WriteLine","usage");
 9:     Console.WriteLine 10: ("{0} plus {1} gives {2}", ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.