[. NET] C # efficient programming (1 ),

Source: Internet
Author: User

[. NET] C # efficient programming (1 ),
C # language habits directory

  • 1. Use attributes instead of accessible data members
  • 2. Use readonly instead of const)
  • 3. We recommend that you use the is or as operator instead of force type conversion.
  • 4. Use the Conditional feature instead of # if condition Compilation
  • 5. Provide the ToString () method for the Type
  • 6. Understanding the relationship between several equality judgments
  • 7. Understand the trap of GetHashCode ()
  • 8. We recommend that you use the query syntax instead of a loop.
  • 9. Avoid using conversion operators in Apis
  • 10. Use optional parameters to reduce the number of method Overloading
  • 11. Understanding the advantages of short methods

 

1. Use attributes instead of accessible data members

 

2. Use readonly instead of const)

1. C # There are two types of constants: compile-time and run-time constants.

2. Try to use the runtime instead of the compilation runtime.

/// <Summary> /// Number of compilation times /// </summary> public const int Num = 100; /// <summary> /// Number of running times /// </summary> public static readonly int Year = 2017;

3. The compile-time constant can only be used for numbers and strings. the run-time constant is also a constant because it cannot be modified after the constructor is executed.

4. const is more efficient than readonly, but less flexible.

 

3. We recommend that you use the is or as operator instead of force type conversion.

1. as is more efficient and secure than strong conversion.

2. The as operator cannot be used with the value type because the value type cannot be null.

 

4. Use the Conditional feature instead of # if condition Compilation
        public static void Test()        {            string msg = null;            #if DEBUG            msg = "Hi";            #endif            Console.WriteLine(msg);        }

// 1. use the cyclic var foo = new int [100]; for (int I = 0; I <100; I ++) {foo [I] = I * I ;} // use the query syntax var foo2 = (from n in Enumerable. range (0,100) select n * n ). toArray ();

1. Some method syntaxes do not have corresponding query syntaxes, such as Take, TaskWhile, Skip, SkipWhile, Min, and Max. You need to use the method syntax.

 

9. Avoid using conversion operators in Apis

 

10. Use optional parameters to reduce the number of method Overloading

1. For the first release of an assembly, you can use optional parameters and named parameters at will. During subsequent release, you must create an overload for additional parameters. In this way, the current program can still run normally. In addition, you should avoid modifying the parameter name in any subsequent releases, because the parameter name has become part of the public interface.

 

11. Understanding the advantages of short methods

1. We 'd better compile the clearest code as much as possible and hand over the optimization work to JIT. A common error optimization is that we put a lot of logic in a function to reduce the additional method call overhead.

        public string Test(bool isTrue)        {            var sb = new StringBuilder();            if (isTrue)            {                sb.AppendLine("A");                sb.AppendLine("B");                sb.AppendLine("C");            }            else            {                sb.AppendLine("E");                sb.AppendLine("F");                sb.AppendLine("G");            }            return sb.ToString();        }

When the Test method is called for the first time, the two branches of if-else are compiled by JIT, but only one of them needs to be compiled. After modification:

        public string Test2(bool isTrue)        {            var sb = new StringBuilder();            if (isTrue)            {                return Method1();            }            else            {                return Method2();            }        }

Now we have split the methods, and the two methods can be compiled by JIT as needed, instead of all compiling for the first time.

2. The if-else branch can contain more than dozens of statements, or a branch is used to handle program errors, or the code in each case in the switch statement can be selectively extracted.

3. The short and concise method (generally containing fewer local variables) makes it easier for JIT to select registers, that is, to select which local variables are placed in registers rather than stacks.

4. Try to write short and concise methods.

 

 

[Blogger] Anti-Bot

Http://www.cnblogs.com/liqingwen/p/6754401.html.

[Reference] C # efficient programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.