C# QA Part - Three

Q. Are any performance tricks related to the many data types?

A. One trick when using whole numbers (values with no decimal places) is to use the data type that matches your processor. For

instance, most current home and office computers have 32-bit processors. The Visual C# Integer data type is made up of 32 bits.

Believe it or not, Visual C# can process an int variable faster than it can process a short variable, even though the short variable is

smaller. This has to do with the architecture of the CPU, memory, and bus. The explanation is complicated, but the end result is that

you should usually use int rather than short, even when working with values that don't require the larger size of the int.

Q. Are arrays limited to two dimensions?

A. Although I showed only two dimensions (that is, intMeasurements[3,1]), arrays can have many dimensions, such as

intMeasurements[3,3,3,4]. The technical maximum is 60 dimensions, but you probably won't use more than three.

Q. Should I always specify parentheses to ensure that operators are evaluated as I expect them to be?

A. Visual C# never fails to evaluate expressions according to the order of operator precedence, so using parentheses isn't necessary

when the order of precedence is correct for an expression. However, using parentheses assures you that the expression is being

evaluated the way you want it to, and might make the expression easier to read by other people. This really is your choice.

Q. I would like to learn more about the properties and methods available in the DateTime structure; where can I find all the members


A. I would look at the DateTime members documentation found within the .NET Framework documentation. This is available on the

MSDN site and as an installable option when installing Visual C#.

Q. What if I want to execute code only when an expression in an if statement is false, not true? Do I need to place the code in an else

clause, and no code after the if?

A. This is where Boolean logic helps. What you need to do is make the expression evaluate to true for the code you want to run. This

is accomplished using the not operator (!) in the expression, like this:

if (!expression)
. . .

Q. How important is the order in which case statements are created?

A. This all depends on the situation. In the earlier example in which the selected animal was considered and the number of legs it has

was displayed, the order of the Dog and Horse case was important. If all case statements contained code, the order has no effect.

Q. Is there ever a situation where you would want a loop to run indefinitely?

A. Game programmers often create a single loop that runs indefinitely, and all logic and user input takes place in this main loop. Other

than such a very specific situation, all loops should terminate at some point.

Q. Should I be concerned about the performance differences between the two types of loops?

A. With today's fast processors, chances are good that the performance difference between the two loop types in any given situation

will be overshadowed by the readability and functionality of the best choice of loop. If you have a situation in which performance is

critical, write the loop using all the ways you can think of, benchmark the results, and choose the fastest loop.

Q. Should I alert the user that an exception has occurred or just let the code keep running?

A. If you've written code to handle the specific exception, there's probably no need to tell the user about it. However, if an exception

occurs that the code doesn't know how to address, you should provide the user with the exception information so that they can

report the problem accurately so that you can fix it.

Q. Should I comment every statement in my application?

A. Probably not. However, consider commenting every decision-making and looping construct in your program. Such sections of

code are usually pivotal to the success of the procedure, and what they do isn't always obvious.

No comments: