Parsing Text to Objects

Converting objects to a textual representation is one thing, dealing with user (or text file and so on) input and turning it into rich objects is another thing. You’ve already seen a whole set of Parse, TryParse, and Exact variants, but you want to note a few more things.

Sometimes going back and forth between objects and text is not a lossless conversion. In such cases, the framework often provides a format string named R to emit a text representation that will restore the original value when using a Parse method. A good example is floating-point numbers, whose decimal representation is not exact:

double value = 1.0 / 3.0;string text = value.ToString();Console.WriteLine(text);               // ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.