CHAPTER 3 C# LANGUAGE FUNDAMENTALS (Web hosting billing) 75 The
CHAPTER 3 C# LANGUAGE FUNDAMENTALS 75 The first parameter to WriteLine() represents a string literal that contains optional placeholders designated by {0}, {1}, {2}, and so forth (curly bracket numbering always begins with zero). The remaining parameters to WriteLine() are simply the values to be inserted into the respective placeholders (in this case, an int, a double, and a bool). Also be aware that WriteLine() has been overloaded to allow you to specify placeholder values as an array of objects. Thus, you can represent any number of items to be plugged into the format string as follows: // Fill placeholders using an array of objects. object[] stuff = {”Hello”, 20.9, 1, “There”, “83″, 99.99933} ; Console.WriteLine(”The Stuff: {0} , {1} , {2} , {3} , {4} , {5} “, stuff); It is also permissible for a given placeholder to repeat within a given string. For example, if you are a Beatles fan and want to build the string “9, Number 9, Number 9″ you would write // John says… Console.WriteLine(”{0}, Number {0}, Number {0}”, 9); Note If you have a mismatch between the number of uniquely numbered curly-bracket placeholders and fill arguments, you will receive a FormatException exception at runtime. .NET String Formatting Flags If you require more elaborate formatting, each placeholder can optionally contain various format characters (in either uppercase or lowercase), as seen in Table 3-3. Table 3-3. .NET String Format Characters String Format Character Meaning in Life C or c Used to format currency. By default, the flag will prefix the local cultural symbol (a dollar sign [$] for U.S. English). D or d Used to format decimal numbers. This flag may also specify the minimum number of digits used to pad the value. E or e Used for exponential notation. F or f Used for fixed-point formatting. G or g Stands for general. This character can be used to format a number to fixed or exponential format. N or n Used for basic numerical formatting (with commas). X or x Used for hexadecimal formatting. If you use an uppercase X, your hex format will also contain uppercase characters. These format characters are suffixed to a given placeholder value using the colon token (e.g., {0:C}, {1:d}, {2:X}, and so on). Assume you have updated Main() with the following logic: // Now make use of some format tags. static void Main(string[] args) { … Console.WriteLine(”C format: {0:C}”, 99989.987); Console.WriteLine(”D9 format: {0:D9}”, 99999); Console.WriteLine(”E format: {0:E}”, 99999.76543); Console.WriteLine(”F3 format: {0:F3}”, 99999.9999);
In case you need quality webspace to host and run your web applications, try our personal web hosting services.