Query syntax allowsProgramLogical expressions are converted from imperative to declarative ". The query syntax defines the desired result and submits the specific implementation to other special implementations.You can use the query syntax (or the method syntax that implements the query expression mode) to clearly express your intent than the conventional imperative loop results.
Next, we observe that an array is filled with an imperative method and its content is output to the console:
1 Static Void Main ( String [] ARGs) 2 { 3 Int [] Foo = New Int [100 ]; 4 For ( Int Num = 0 ; Num <Foo. length; num ++ ) 5 { 6 Foo [num] = num * Num; 7 } 8 Foreach (Int I In Foo) 9 { 10 Console. writeline (I ); 11 } 12 Console. Read (); 13 }
Write imperativeCodeYou need to pay attention to the specific implementation details. However, if the query syntax is used to implement the same function, the code is easier to reuse and read. Let's look at the statement of the above example "declarative:
1 Static Void Main ( String [] ARGs) 2 { 3 // An array is generated and a query is completed. 4 Int [] Foo = ( From N In Enumerable. Range ( 0 , 100 )Select N * N). toarray (); 5 6 // Loop printing is done by an Array Extension Method. 7 Foo. forall (n) => Console. writeline (N. tostring ())); 8 9 Console. Read (); 10 }
We can see that we use an extension method in charge of loop printing, which brings better reusability. Each time we need to execute an operation on the elements of a sequence, we can use forall () method:
1 Public Static Class Extensions 2 { 3 /// <Summary> 4 /// Add Extension Method for ienumerable <t> type 5 /// </Summary> 6 /// <Typeparam name = "T"> </typeparam> 7 /// <Param name = "sequence"> </param> 8 /// <Param name = "action"> </param> 9 Public Static Void Forall <t> ( This Ienumerable <t> sequence, Action <t> Action) 10 { 11 Foreach (T item In Sequence) 12 { 13 Action (item ); 14 } 15 } 16 }
The above instance is relatively simple and does not seem to see much difference between the two. In the following instance, we use the "imperative" and "declarative" to compare the differences between the two.
Imperative:
View code
1 Private Static Ienumerable <tuple < Int , Int > Produceindices () 2 { 3 # Region Use an integer ranging from 0 to 99 to produce the (x, y) Binary Group. 4 5 // For (INT x = 0; x <100; X ++) 6 // For (INT y = 0; y <100; y ++) 7 // Yield return tuple. Create (x, y ); 8 9 # Endregion 10 11 # Region The sum of X and Y must be less than 100 12 13 // For (INT x = 0; x <100; X ++) 14 // For (INT y = 0; y <100; y ++) 15 // If (x ++ Y <100) 16 // Yield return tuple. Create (x, y ); 17 18 # Endregion 19 20 # Region Binary groups are arranged in reverse order based on their distance away from each other. 21 22 VaR Storage = New List <tuple < Int , Int > (); 23 24 For ( Int X = 0 ; X < 100 ; X ++ ) 25 For ( Int Y = 0 ; Y < 100 ; Y ++ ) 26 If (X + y < 100 ) 27 Storage. Add (tuple. Create (x, y )); 28 29 Storage. Sort (point1, point2) => (point1.item1 * point2.item1 + point2.item2 * point2.item2). compareto (point1.item1 * point1.item1 + point1.item2 * Point1.item2 )); 30 Return Storage; 31 32 # Endregion 33 }
Declarative:
View code
1 Private Static Ienumerable <tuple <Int , Int > Queryindices () 2 { 3 # Region Use an integer ranging from 0 to 99 to produce the (x, y) Binary Group. 4 5 // Return from X in enumerable. Range (0,100) 6 // From y in enumerable. Range (0,100) 7 // Select tuple. Create (x, y ); 8 9 # Endregion 10 11 # Region The sum of X and Y must be less than 100 12 13 // Return from X in enumerable. Range (0,100) 14 // From y in enumerable. Range (0,100) 15 // Where X + Y <100 16 // Select tuple. Create (x, y ); 17 18 # Endregion 19 20 # Region Binary groups are arranged in reverse order based on their distance away from each other. 21 22 Return From X In Enumerable. Range ( 0 , 100 ) 23 From Y In Enumerable. Range ( 0 , 100 ) 24 Where X + Y < 100 25 Orderby (X * x + y * Y) descending 26 Select Tuple. Create (x, y ); 27 28 # Endregion 29 }
We can see that with the complexity of programming tasks:
"Imperative"Versions become increasingly difficult to understand. If you take a closer look, you won't even find that the parameters in the comparison function are reversed (this is an error), and this is only for descending order. Without any comments and stability, the imperative code will be more difficult to read ." The imperative "code too emphasizes the detailed steps required to achieve the goal, so that people can easily fall into specific details.
"Declarative"The last Implementation of the version simply combines this filter (where clause), this sort (orderby clause), and a projection (select. The query syntax provides more composite APIs than the loop structure. The query syntax naturally includesAlgorithmIt is broken down into small pieces of code, and each piece of code quietly performs a single operation on the elements in the sequence. The delayed execution mode of the query syntax allows developers to combine these single operations into multi-step operations, and the execution can be complete as long as the traversal sequence is reached, the cyclic syntax structure must create temporary storage for each operation step, or create a dedicated method for each batch of operations to be executed.
Section:
When you need to write a loop, first check whether it can be implemented using the query syntax. If the query syntax cannot be used, then check whether it can be replaced by the method call syntax. The code written in this way is always simpler than the imperative loop structure.