Free mag vol1 | Page 158

CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I Members of System.Boolean Next, consider the System.Boolean data type. The only valid assignment a C# bool can take is from the set {true | false}. Given this point, it should be clear that System.Boolean does not support a MinValue/MaxValue property set, but rather TrueString/FalseString (which yields the string "True" or "False", respectively). For example: Console.WriteLine("bool.FalseString: {0}", bool.FalseString); Console.WriteLine("bool.TrueString: {0}", bool.TrueString); Members of System.Char C# textual data is represented by the string and char keywords, which are simple shorthand notations for System.String and System.Char, both of which are Unicode under the hood. As you might already know, a string represents a contiguous set of characters (e.g., "Hello"), while the char can represent a single slot in a string (e.g., 'H'). The System.Char type provides you with a great deal of functionality beyond the ability to hold a single point of character data. Using the static methods of System.Char, you are able to determine whether a given character is numerical, alphabetical, a point of punctuation, or whatnot. Consider the following method: static void CharFunctionality() { Console.WriteLine("=> char type Functionality:"); char myChar = 'a'; Console.WriteLine("char.IsDigit('a'): {0}", char.IsDigit(myChar)); Console.WriteLine("char.IsLetter('a'): {0}", char.IsLetter(myChar)); Console.WriteLine("char.IsWhiteSpace('Hello There', 5): {0}", char.IsWhiteSpace("Hello There", 5)); Console.WriteLine("char.IsWhiteSpace('Hello There', 6): {0}", char.IsWhiteSpace("Hello There", 6)); Console.WriteLine("char.IsPunctuation('?'): {0}", char.IsPunctuation('?')); Console.WriteLine(); } As illustrated in the previous method, many members of System.Char have two calling conventions: a single character, or a string with a numerical index that specifies the position of the character to test. Parsing Values from String Data The .NET data types provide the ability to generate a variable of their underlying type given a textual equivalent (e.g., parsing). This technique can be extremely helpful when you wish to convert a bit of user input data (such as a selection from a GUI-based, drop-down list box) into a numerical value. Consider the following parsing logic within a method named ParseFromStrings(): static void ParseFromStrings() { Console.WriteLine("=> Data type parsing:"); 92