日期:2008-04-18  浏览次数:20500 次

    1. Do not rely on exceptions in your code: Exceptions are very expensive and should rarely occur in your code. You should never use exceptions as a way to control normal program flow. If it is possible to detect in code a condition that would cause an exception, you should do that instead of waiting to catch the exception before handling that condition. Common scenarios include checking for null, assigning to a string that will be parsed into a numeric value, or checking for specific values before applying math operations. For example:

    1. // Consider changing this:try {   result = 100 / num;}catch (Exception e) {  result = 0;}// To this:if (num != 0)   result = 100 / num;else  result = 0;
    ' Consider changing this:Try   result = 100 / numCatch (e As Exception)  result = 0End Try// To this:If Not (num = 0)   result = 100 / numElse  result = 0End If
    // Consider changing this:try {   result = 100 / num;}catch (e:Exception) {  result = 0;}// To this:if (num != 0)   result = 100 / num;else  result = 0;
    C# VB JScript

    1. Use early binding in Visual Basic or JScript code: One of the advantages of Visual Basic, VBScript, and JScript is their typeless nature. Variables can be created simply by using them and need no explicit type declaration. When assigning from one type to another, conversions are performed automatically, as well. This can be both an advantage and a disadvantage, since late binding is a very expensive convenience in terms of performance.
    2. The Visual Basic language now supports type-safe programming through the use of a special Option Strict compiler directive. For backward compatibility, ASP.NET does not enable Option Strict by default. However, for optimal perfomance, you should enable Option Strict for your pages by using a Strict attribute on the page or Control directive:
    3. <%@ Page Language="VB" Strict="true" %><%Dim BDim C As String' This causes a compiler error:A = "Hello"' This causes a compiler error:B = "World"' This does not:C = "!!!!!!"' But this does:C = 0%>
    4. JScript also supports typeless programming, though it offers no compiler directive to force early binding. A variable is late-bound if:
    5. It is declared explicitly as an object.
    • It is a field of a class with no type declaration.
    • It is a private function/method member with no explicit type declaration and the type cannot be inferred from its use.
    1. The last distinction is complicated. The JScript compiler optimizes if it can figure out the type, based on how a variable is used. In the following example, the variable A is early-bound but the variable B is late-bound:
    2. var A;var B;A = "Hello";B = "World";B = 0;
    3. For the best performance, declare your JScript variables as having a type. For example, "var A : String".
    4. Port call-intensive COM components to managed code: The .NET Framework provides a remarkably easy way to interoperate with traditional COM components. The benefit is that you can take advantage of the new platform while preserving your existing code. However, there are some circumstances in which the performance cost of keeping your