Short-circuit logic and delayed Loading

Source: Internet
Author: User
I. Short Circuit Logic

Short-circuit logic is also called lazy evaluation. It has an interesting feature in well-known Boolean operations: the value is evaluated only when the value is required. For example, conditiona () and conditionb () are true when both conditions are true. Therefore, if conditiona () is false, the expression returns false immediately instead of conditionb () this results in unnecessary computing waste.

Static void main (string [] ARGs) {If (conditiona () & conditionb () {system. console. writeline ("conditiona () & conditionb () is true");} system. console. writeline ("short-circuit logic test end"); system. console. read ();} static bool conditiona () {system. console. writeline ("conditiona: false"); Return false;} static bool conditionb () {system. console. writeline ("conditionb: True"); Return true ;}

Output:

Conditiona: false

Short-circuit logic test end

As you can see, if conditiona () is false, then conditionb () is "short-circuited" and does not need to be executed. This "evaluate only when the value is required" feature reflects the "On-Demand" idea and can be seen as a simple delayed loading.

 

Ii. Delayed loading (execution)

In actual development, the idea of delayed loading is widely used, such as loading on demand of slice, loading of associated data by query implemented by some Orm, and so on. So what is delayed loading?

Baidu encyclopedia's explanation is terrible, but we can all understand that delayed loading reflects the core idea of "creating resources to use on demand. In. net, there are many implementation mechanisms for load on demand (load on demand) Delayed loading. The following are two common examples of delayed loading based on actual development.

1. Yield and LINQ

You can refer to this article for more information about how to use the yield keyword.

Yield statements can only appear in iterator (iterator) blocks. We can put some time-consuming or delayed operations in movenext. It is yield that makes it possible to delay the execution of LINQ.

In LINQ, all the classes that implement the ienumerable <t> interface are called sequence (sequence). The most basic data units of LINQ are sequences and elements. A sequence is an object that implements ienumerable <t>, and an element is every element in the sequence. The following is a simple LINQ query:

 class userinfo {public string name {Get; Set ;}} class program {static void main (string [] ARGs) {var lang = "Python"; var list = new list 
  
    {New userinfo {name = "jeffwong "}, new userinfo {name = "anytao"}, new userinfo {name = "Dudu" }}; var query = List. where (M => M. name. startswith ("J"); // LINQ query if (lang = "C #") {foreach (VAR item in query) {console. writeline (item. name) ;}} system. console. read () ;}
  

Example aboveCodeThere is an if condition, that is, the name of the user (element) in the output sequence when the language (Lang) is equal to C. But in fact, this condition is false, so it does not have any effect on the query results of the LINQ query. If this is a time-consuming query operation, but the query results are not used, isn't it done in white? Fortunately, in the implementation of LINQ query, the query variable is only a placeholder for the ienumerable object. It is neither computed nor loaded (here user information) in the memory, the value is calculated only when the foreach traversal value is used, which obviously saves the cost.

Of course, in the above example, the List data size we constructed is very small, and it is in the memory. Even if the query is executed, it seems that there is not much improvement in performance. But in fact, in the well-known LINQ to SQL, iqueryable <t> inherits and implements the ienumerable <t> interface, and uses LINQ to SQL to access the database, the impact on network transmission and I/O must be considered. Otherwise, slow query may cause poor user experience. Fortunately, we can use the delay execution mechanism of LINQ to write a good query, and both the Microsoft LINQ to SQL and the ADO. NET Entity Framework framework support the delay loading mechanism for associated data.

 

2. Lazy <t>

In. net, lazy <t> is a typical class for loading on-demand objects with a delay. It is very easy to create a "lazy object:

Class userinfo {public string name {Get; Set ;}} class program {static void main (string [] ARGs) {lazy <userinfo> lazyuser = new lazy <userinfo> (() => New userinfo {name = "jeffwong"}); If (! Lazyuser. isvaluecreated) // The user has not created {console. writeline ("user is not created yet. ");} console. writeline (lazyuser. value. name); // create the user system. console. read ();}}

In fact, lazy <t> can provide developers with more control options (such as support for delegation) and thread security.

 

Refer:

Http://msdn.microsoft.com/en-us/library/dd642331.aspx

Http://msdn.microsoft.com/en-us/library/9k7k7cf0 (V = vs.100). aspx

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.