Effective C#50 Specific Ways to Improve Your C# Second Edition phần 3

Các ngôn ngữ khác, đặc biệt là , đã xác định cú pháp truy vấn cho rất nhiều các từ khóa này. Đây là một phần của bất kỳ cuộc thảo luận có người khẳng định rằng các truy vấn thực hiện chậm hơn so với các vòng khác. Trong khi bạn chắc chắn có thể tạo ra các ví dụ một vòng tay được mã hóa sẽ tốt hơn một truy vấn, | 56 I Chapter 1 C Language Idioms level. Other languages in particular did define query syntax for many of these keywords. This is the part of any discussion where someone usually asserts that queries perform more slowly than other loops. While you can certainly create examples where a hand-coded loop will outperform a query it s not a general rule. You do need to measure performance to determine if you have a specific case where the query constructs don t perform well enough. However before completely rewriting an algorithm consider the parallel extensions for LINQ. Another advantage to using query syntax is that you can execute those queries in parallel using the .AsParallel method. See Item 35. C began as an imperative language. It continues to include all the features that are part of that heritage. It s natural to reach for the most familiar tools at your disposal. However those tools might not be the best tools. When you find yourself writing any form of a looping construct ask yourself if you can write that code as a query. If the query syntax does not work consider using the method call syntax instead. In almost all cases you ll find that you create cleaner code than you would using imperative looping constructs. Item 9 Avoid Conversion Operators in Your APIs Conversion operators introduce a kind of substitutability between classes. Substitutability means that one class can be substituted for another. This can be a benefit An object of a derived class can be substituted for an object of its base class as in the classic example of the shape hierarchy. You create a Shape base class and derive a variety of customizations Rectangle Ellipse Circle and so on. You can substitute a Circle anywhere a Shape is expected. That s using polymorphism for substitutability. It works because a circle is a specific type of shape. When you create a class certain conversions are allowed automatically. Any object can be substituted for an instance of the root

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.