Home > Articles > Programming > Windows Programming

Hello, C#

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

1.6 Formatting Output

In addition to writing each line of text to the output file, let's extend the previous code segment to also write the line to the user's console. When writing to the console, however, we want to indicate the line number and the length of the line in characters, as well as the text itself. In addition, we don't want to echo an empty line. Here is the relevant portion of the modified code segment:

string text_line;
int  line_cnt = 1;

while (( text_line = freader.ReadLine() ) != null )
{
	 // don't format empty lines
	 if ( text_line.Length == 0 )
	 {
		 Console.WriteLine();
		 continue;
	 }


	 // format output to console:
	 // 1 (42): Master Timothy Gnome left home one morning

 	 Console.WriteLine( "{0} ({2}): {1}",
       line_cnt++, text_line, text_line.Length );
}

The continue statement allows us to short-circuit the remaining portion of the loop body. In this example, if the text line is empty, we write a new line to the console and then prematurely terminate this iteration of the loop body before writing the line of text to the console. The continue statement causes the next iteration of the loop to begin immediately.

Similarly, a break statement causes the premature termination of the loop itself. For example, we might use the break statement when we are iterating through a collection searching for a value. Once the value has been found, we break out of the loop. An extreme example of this idiom is the nonterminating condition test for a loop—for example,

while ( true )
{

	 // process until some condition occurs

	 // ...

	 if ( condition_occurs )
		break;
}

The assumption is that an internal state of the class or application causes the eventual invocation of the break statement, terminating the loop.

WriteLine() allows us to pass positional arguments within a literal string—for example,

Console.WriteLine( "Hello, {0}! Welcome to C#", user_name );

When a number enclosed within curly braces, such as {0}, is encountered within the literal string, it's treated as a placeholder for the associated value in the list of parameters that follows the literal string. 0 represents the first value, 1 represents the second value, and so on. The numbered placeholders can appear in any order, as in this example:

 Console.WriteLine( "{0} ({2}): {1}",
	 line_cnt++, text_line, text_line.Length );

The same position value can appear multiple times as well—for example,

Console.WriteLine( "Hello, {0}! Everyone, please welcome {0}",
			 user_name );

We can control numeric formatting of the built-in types through the addition of format characters. For example, a C interprets the value in terms of the local currency, F indicates a fixed-point format, E indicates an exponential (scientific) format, and G leaves it to the system to pick the most compact form. Each may be followed by a value specifying the precision. For example, given the object

double d = 10850.795;

the WriteLine() statement

Console.WriteLine("{0} : {0:C2} : {0:F4} : {0:E2} : {0:G}",d);

generates the following output:

10850.795 : $10,850.80 : 10850.7950 : 1.09E+004 : 10850.795

We can use X or x to output the value in hexadecimal. X results in the uppercase values A through F; x results in lowercase a through f.

  • + Share This
  • 🔖 Save To Your Account