Efficiency of the Loop Structure in C #

Source: Internet
Author: User

Introduction
Sequence, selection, and loop are the three structures of all programs. Today, let's talk about a small problem in the loop.

Content
There are four types of c # cycle structures:

For
While... Do...
Do... While...
Foreach
In these four structures, the cycle ends. The value must be greater than a number, less than a number, or other conditional expressions. Today, let's talk about the determination of numbers.

Our numbers may be stored in a defined variable, the length of a set, or the information returned by a method. Here we will discuss the returned information of a method.

Assuming the following method exists, the returned value is a List <int>

Static List <int> GetIntList ()
{
Console. WriteLine ("the {0} to enter the GetIntList method", Counter );
Counter ++;
Return new List <int> (){
1, 2, 3, 4, 5 };
} The returned results of this method need to be processed cyclically. Most of the results are written in this way at the beginning. The for loop is used as an example.

For (int I = 0; I <GetIntList (). Count; I ++)
{
} I learned more about it later, or I heard someone else say that it has been verified by myself. The GetIntList method will be re-called every time a problem is found in this loop judgment condition, resulting in a great waste. It will be changed to the following.

Int len = GetIntList (). Count;
For (int I = 0; I <len; I ++)
{
} Define a variable to save the length of the set.

Verified, while... Do, do... While also has a similar problem, you also need to note.

Sometimes what we need to do is to process each element in the set. We will use the convenient loop structure of foreach, because he does not need to consider the issue of subscripts crossing the border. From the above verification results, someone will say that the following code has a problem.

Foreach (int I in GetIntList ())
{
} Is there any problem? Verification is required.

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Data. Common;
Using System. Configuration;
Using MongoDB. Driver;
Using AutoTest. ServiceLocator;
Using System. Reflection;
Using AutoTest. Common;

Namespace AutoTest
{
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("-------------------------------- Begin ------------------------------");


Console. WriteLine ("Foreach loop ");
Counter = 1;
Foreach (int I in GetIntList ())
{
}
Console. WriteLine ("For Loop ");
Counter = 1;
For (int I = 0; I <GetIntList (). Count; I ++)
{
}
Console. WriteLine ("do... while... loop ");
Counter = 1;
Int num = 0;
Do
{
Num ++;
}
While (num <GetIntList (). Count );
Console. WriteLine ("while... do... loop ");
Counter = 1;
Num = 0;
While (num <GetIntList (). Count)
{
Num ++;
};

Console. WriteLine ("------------------------------ End ------------------------------");
Console. ReadKey ();

}
Static int Counter = 1;
Static List <int> GetIntList ()
{
Console. WriteLine ("the {0} to enter the GetIntList method", Counter );
Counter ++;
Return new List <int> (){
1, 2, 3, 4, 5 };
}
}


} In fact, after verification, we found that, unlike the other three loop structures, there will be no repeated call of the GetIntList method.
 

Conclusion
1. foreach and the other three loops have different structures.

2. Use facts to speak.

 

 

Related Article

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.