var jagged = new[] {
new [ ] { 1, 2, 3, 4 }, new[] { 9, 8, 7 },
new [ ] { 11, 12, 13, 14, 15 }
};
, new [ ] . -, . -, , . , . , , .
, LINQ- . .
f oreach
5, C# f oreach, . .
f oreach , . C# , . foreach.
foreach ( __ in ) ;
__ , foreach. , . , . , var. , . , . , , .
foreach . , . , . , . , foreach .
, , , foreach . , , , .
foreach. , , .
// foreach.
Using System;
class ForeachDemo { static void Main() {
int sum = 0;
int[] nums = new int [10];
|
|
// nums. for(int i = 0; i < 10; i++) nums[i] = i;
// foreach // , foreach(int in nums) {
Console.WriteLine(" : " + ); sum += ;
}
Console.WriteLine(" : " + sum);
}
}
.
:
:
:
:
:
:
:
:
:
:
: 45
, foreach .
foreach , , , break. , nums.
// break foreach.
Using System;
class ForeachDemo { static void Main() { int sum = 0;
int[] nums = new int [10];
// nums. for(int i = 0; i < 10; i++) nums[i] = i;
// foreach // .
foreach(int x in nums) {
Console.WriteLine(" : " + x); sum += x;
if(x == 4) break; // , 4
}
Console.WriteLine(" 5 : " + sum);
}
}