Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
2.1.5 điều hành ưu tiên Trong C #, như trong hầu hết các ngôn ngữ lập trình khác, các nhà khai thác có quy định khác nhau của ưu tiên là các so sánh nhà điều hành làm việc từ trái sang phải trong khi các nhà khai thác chỉ định phải sang trái Hãy xem xét những biểu hiện trong | 20 Applied C in Financial Markets before the code body has executed. In Example 2.33 while is used with an enumerator with the MoveNext method which will return true until there are no more records to be processed. Example 2.33 while loop shown in context of an enumerator while fxE.MoveNext this.lstFX.Items.Add fxE.Key t fxE.Value 2.3.4 do while do code body while condition do while is similar to while the big difference being in evaluating the condition. In do while the condition is examined after the code body in the loop has run whereas while evaluates the condition before looping. In Example 2.34 a file is being read until the line returned is null note that the ReadLine is executed before the check that line is null. Example 2.34 do while loop evaluating the line after the loop being processed do line sIn.ReadLine if line null string ccFX rExp.Split line _rates.Add ccFX 0 ccFX 1 while line null 2.3.5 for loop for initialise counter exit condition counter code body The block of code in a for loop iterates around with the counter being initialised the condition checking and the counter all being on the same line. The Basics of C 21 In Example 2.35 the for loop begins at zero and loops around until the condition I _initialPool is met the example shows the for loop used in initialising a number of database connections. Example 2.35 For loop showing a number of connections being initialised private void initConnections for int i 0 i _initialPool i addConnection i 2.3.6 foreach loop foreach element in collection code body The foreach loop is used to iterate through either collections or arrays and goes through each element until the last one is reached. In writing a foreach loop the structure of the collection should remain unchanged otherwise the loop could cause some unpredictable results. Example 2.36 shows a collection being created and the elements being iterated through. The example is taken from a class that builds a dynamic string of SQL. Example 2.36 .