Đang chuẩn bị liên kết để tải về tài liệu:
Effective C#50 Specific Ways to Improve Your C# 2nd phần 2

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Sử dụng # và # endif pragmas, bạn đã tạo ra một phương pháp có sản phẩm nào trong lần phát hành các bản xây dựng. CheckState () phương pháp được gọi là trong tất cả các phiên bản, xây dựng và gỡ lỗi. Nó không làm bất cứ điều gì trong bản phát hành xây dựng, nhưng bạn phải trả cho các cuộc gọi phương pháp. | 22 I Chapter 1 C Language Idioms Trace.WriteLine Exiting Checkstate for Person endif Using the if and endif pragmas you ve created an empty method in your release builds. The CheckState method gets called in all builds release and debug. It doesn t do anything in the release builds but you pay for the method call. You also pay a small cost to load and JIT the empty routine. This practice works fine but can lead to subtle bugs that appear only in release builds. The following common mistake shows what can happen when you use pragmas for conditional compilation public void Func string msg null if DEBUG msg GetDiagnostics endif Console.WriteLine msg Everything works fine in your debug build but your release builds happily print a blank message. That s not your intent. You goofed but the compiler couldn t help you. You have code that is fundamental to your logic inside a conditional block. Sprinkling your source code with if endif blocks makes it hard to diagnose the differences in behavior with the different builds. C has a better alternative the Conditional attribute. Using the Conditional attribute you can isolate functions that should be part of your classes only when a particular environment variable is defined or set to a certain value. The most common use of this feature is to instrument your code with debugging statements. The .NET Framework library already has the basic functionality you need for this use. This example shows how to use the debugging capabilities in the .NET Framework Library to show you how conditional attributes work and when to add them to your code. When you build the Person object you add a method to verify the object invariants From the Library of Wow eBook Item 4 Use Conditional Attributes Instead of if I 23 private void CheckState Grab the name of the calling routine string methodName new StackTrace .GetFrame 1 .GetMethod .Name Trace.WriteLine Entering CheckState for Person Trace.Write tcalled by Trace.WriteLine methodName Debug.Assert

TÀI LIỆU LIÊN QUAN