Parameters
Parameters
i. value parameters,
ii. reference parameters,
iii. output parameters, and
iv. parameter arrays.
The initializer section that is executed only once, before entering the loop. Typically, you declare and
initialize a local loop variable in that section. The declared variable can't be accessed from outside the for
statement.
The initializer section in the preceding example declares and initializes an integer counter variable: int i = 0
The condition section that determines if the next iteration in the loop should be executed.
If it evaluates to true or isn't present, the next iteration is executed; otherwise, the loop is exited.
The condition section must be a Boolean expression.
The condition section in the preceding example checks if a counter value is less than three: i < 3;
The iterator section that defines what happens after each execution of the body of
the loop.
The iterator section in the preceding example increments the counter: i++
The body of the loop, which must be a statement or a block of statements.
The iterator section can contain zero or more of the following statement expressions,
separated by commas:
1. prefix or postfix increment expression, such as ++i or i++
2. prefix or postfix decrement expression, such as --i or i--
3. assignment
4. invocation of a method
5. await expression
6. creation of an object by using the new operator
If you don't declare a loop variable in the initializer section, you can use zero or more of
the expressions from the preceding list in the initializer section as well.
The following example shows several less common usages of the initializer and iterator sections:
assigning a value to an external variable in the initializer section, invoking a method in
both the initializer and the iterator sections, and changing the values of two variables in
the iterator section:
All the sections of the for statement are optional. For example, the following code
defines the infinite for loop:
C#
for ( ; ; )
{
//...
}
The foreach statement
The foreach statement executes a statement or a block of statements for each element in an instance of
the type that implements
the System.Collections.IEnumerable or
System.Collections.Generic.IEnumerable<T> interface, as the following example shows:
Started time:11:30 A.M---9-Feb 2024
The foreach statement isn't limited to those types. You can use it with an instance of
any type that satisfies the following conditions:
A type has the public parameterless GetEnumerator method. The GetEnumerator
method can be a type's extension method.
The return type of the GetEnumerator method has the public Current property and
the public parameterless MoveNext method whose return type is bool .
If the enumerator's Current property returns a reference return value ( ref T where T is
the type of a collection element), you can declare an iteration variable with the ref or
ref readonly modifier, as the following example shows:
var keyword:
can use the var keyword to let the compiler infer the type of an iteration variable in
the foreach statement.
Note: Type of var can be inferred by the compiler as a nullable reference type,
depending on whether the nullable aware context is enabled and whether the type
of an initialization expression is a reference type.