Primitive Value Types

We have already seen all of the primitive value types supported by the C# language, using suffixes for literals (for example, 123L for a long) and various operations defined on them. However, some operations require use of static or instance members on those types. Let’s take a look.

Parsing Strings

One rather common operation used to obtain a primitive value is to parse external input, for example, coming from a database, directly from the user, or text-based stores such as XML files. In such a case, parsing a textual representation of a value into a proper value is needed.

Two methods exist on most primitive types that allow for precisely this: Parse and TryParse. Their behavior differs in whether an exception gets thrown ...

Get C# 5.0 Unleashed 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.