Rather than returning the parsed value it returns a

Info icon This preview shows pages 360–363. Sign up to view the full content.

View Full Document Right Arrow Icon
Rather than returning the parsed value, it returns a bool which indicates whether the parse was successful. The parsed value is retrieved via an out parameter. Exam- ple 10-42 shows that in use. Example 10-42. Avoiding exceptions with TryParse int parsed; if (!int.TryParse("Well, how about that", out parsed )) { Console.WriteLine("That didn't parse"); } 336 | Chapter 10: Strings
Image of page 360

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
For each of the TryParse methods, there is an equivalent Parse , which throws a FormatException on failure and returns the parsed value on success. For many appli- cations, you can use these as an alternative to the Convert methods. Some parse methods can also offer you additional control over the process. Date Time.ParseExact , for example, allows you to provide an exact format specification for the date/time string, as Example 10-43 shows. Example 10-43. DateTime.ParseExact DateTime dt = DateTime.ParseExact("12^04^2008","dd^MM^yyyy",CultureInfo.CurrentCulture); This can be useful if you expect a nonstandard format for your string, coming from a legacy system, perhaps. Composite Formatting with String.Format The previous examples have all turned exactly one piece of information into a single string (or vice versa). Very often, though, we need to compose multiple pieces of in- formation into our final output string, with different conversions for each part. We could do that by composing strings (something we’ll look at later in this chapter), but it is often more convenient to use a helper method: String.Format . Example 10-44 shows a basic example. Example 10-44. Basic use of String.Format int val1 = 32; double val2 = 123.457; DateTime val3 = new DateTime(1999, 11, 1, 17, 22, 25); string formattedString = String.Format("Val1: {0} , Val2: {1} , Val3: {2} ", val1 , val2 , val3 ); Console.WriteLine(formattedString); This method takes a format string, plus a variable number of additional parameters. Those additional parameters are substituted into the format string where indicated by a format item . At its simplest, a format item is just an index into the additional parameter array, enclosed in braces (e.g., {0} ). The preceding code will therefore produce the following output: Val1: 32, Val2: 123.457, Val3: 01/11/1999 17:22:25 A specific format item can be referenced multiple times, and in any order in the format string. You can also apply the standard and custom formatting we discussed earlier to any of the individual format items. Example 10-45 shows that in action. Example 10-45. Using format strings from String.Format int first = 32; double second = 123.457; DateTime third = new DateTime(1999, 11, 1, 17, 22, 25); Formatting Data for Output | 337
Image of page 361
string output = String.Format( "Date: {2 :d }, Time: {2 :t }, Val1: {0}, Val2: {1 :#.## }", first, second, third); Console.WriteLine(output); Notice the colon after the index, followed by the simple or custom formatting string, which transforms the output: Date: 01/11/1999, Time: 17:22, Val1: 32, Val2: 123.46 String.Format is a very powerful technique, but you should be aware that there is some overhead in its use with value types. The additional parameters take the form of an array of objects (so that we can pass in any type for each format item). This means that the values passed in are boxed, and then unboxed. For many applications this overhead
Image of page 362

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 363
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern