Beginning Visual C# Exercises - Chapter 07
1. Maybe.
If you intend code benchmarks in the beta release, then Trace
makes sense but would necessitate clean up before final release.
2. ... At top of code
using System.Diagnostics;
... In Main function
int[] myArray = new int[500];
int i;
for (i = 0; i < 10000; i++)
{
Debug.Assert(i < 4999, "The test loop has reached 5000 iterations",
"An error is pending - please step through debug");
if (i == 4999)
{
Console.WriteLine("This array element {0} is going to break things!", myArray[1000]);
}
}
3. False, a finally code block always executes
4. ... At start of class
enum orientation : byte
{
north = 1,
south = 2,
east = 3,
west = 4
}
... In Main function
/* The checked function tests conversion of data types.
* Assigning a value to "myByte" (as instructions suggested) precludes
* conversion test to "orientation" since both are byte data types.
* Following code tests string input conversion */
try
{
orientation myDirection;
Console.WriteLine("Enter a whole number to consider:");
myDirection = checked((orientation)Convert.ToByte(Console.ReadLi ne()));
Console.WriteLine("Here's your solution: {0}", (orientation)myDirection);
}
catch (OverflowException)
{
Console.WriteLine("The number you entered is out of bounds for the orientation data type");
}
catch (Exception e)
{
Console.WriteLine("An unexpected error resulted from your input: {0}", e.Message);
}
|