: : 1 : 2 : 3 : 4 5 : 10
, foreach .
foreach . , .
// foreach .
using System;
class ForeachDemo2 { static void Main() { int sum = 0;
int[,] nums = new int'[3,5];
// nums. for (int i = 0; i < 3; i++) for (int j=0; j < 5; j++) nums[i,j] = (i+l)*(j+l);
// foreach // , foreach(int in nums) {
Console.WriteLine(" : " + ); sum += ;
}
Console.WriteLine(" : " + sum);
}
}
.
: 1 : 2 : 3 : 4
:
:
:
:
:
:
:
:
:
:
:
: 90
foreach : , , , . . , , . , foreach . , .
// foreach.
using System;
class Search {
static void Main() {
int[] nums = new int [10]; int val;
bool found = false;
// nums. for (int i = 0; i < 10; i++) nums[i] = i;
val = 5;
// foreach // nums. foreach(int in nums) { if(x == val) {
|
|
found = true; break;
}
}
if(found)
Console.WriteLine(" !");
}
}
.
!
foreach , . foreach , , .. , foreach .
string #. . . C# . , string . string C# , , .
string , 2, , . . , :
Console.WriteLine(" C# .");
" C# . " #. , string . , .
. , str .
string str = " C# .";
str " C# .11.
string char. :
char[] charray = {'t', ' ', 's', ' t'};
string str = new string(charray);
string , , , . , string WriteLine ().
// .
Using System;
class StringDemo { static void Main() {
char[] charray ={'', '1, 'o', 1 ', '', ' t', 1 p', 'o', '', 'a',
1 1};
string strl = new string(charray);
string str2 = " .";
Console.WriteLine(strl);
Console.WriteLine(str2);
}
}