You are developing an application that includes the following code segment. (Line numbers are
included for reference only.)
You need to ensure that the application accepts only integer input and prompts the user each time
non-integer input is entered.
Which code segment should you add at line 19?
A.
If (!int.TryParse(sLine, out number))
B.
If ((number = Int32.Parse(sLine)) == Single.NaN)
C.
If ((number = int.Parse(sLine)) > Int32.MaxValue)
D.
If (Int32.TryParse(sLine, out number))
Explanation:
B and C will throw exception when user enters non-integer value. D is exactly the opposite what we
want to achieve.
Int32.TryParse – Converts the string representation of a number to its 32-bit signed integer
equivalent. A return value indicates whether the conversion succeeded.
http://msdn.microsoft.com/en-us/library/f02979c7.aspx
A
A.
each of the primitive types has a parse method that convets the string into that data type.these parsing methods throw exceptions if input is not recognised. u can use a try catch block to protect the program from these exceptions. but each class prov tryparse. method parses a string and returns true if it succeeds and saves the parsed value to an output variable that you pass to the method.