(jagged) . . :
:
static void Main(string[] args)
{
int[][] array = new int[3][]; // , 3
array [0] = new int[3]; //
array [1] = new int[2];
array [2] = new int[5];
}
, c, ( ):
array [0][1] = 5;
array [1][1] = 8;
array [1][2] = 5; // , 2
Length
. Length, ( )
static void Main(string[] args)
{
int[] numbers = new int[5];
int size = numbers.Length; // size = 5
}
List
List , . . , , .
static void Main(string[] args)
{
List<string> teams = new List<string>(); //
teams.Add("Barcelona"); //
teams.Add("Chelsea");
teams.Add("Arsenal");
List<string> teams2 = new List<string>() {"Dynamo", "CSKA" }; //
}
:
Add([]) | |
AddRange([ ]) | |
Insert([],[]) | , |
InsertRange([], [ ]) | , |
Remove([]) | |
RemoveRange([], []) | , |
RemoveAt([]) | , |
Clear() |
Count Length .
static void Main(string[] args)
{
List<string> teams = new List<string>() { "Inter", "Milan", "Bayern", "Juventus"};
teams.Insert(2,"Barcelona"); // "Barcelona" 2
teams.Remove("Milan"); // "Milan"
List<string> newTeams = new List<string>() { "Liverpool", "Roma", "Borussia", "Valencia" };
teams.AddRange(newTeams); // newTeams
}
|
|
, , List. , , .
.
.
- : for, while, do-while, foreach.
for
, , . :
for ( ; ; )
{
// ,
}
, 0, 1, 2, 3, 4:
static void Main(string[] args)
{
for (int i = 0; i < 5; i++) // 5
{
Console.WriteLine(i);
}
}
, i=0. (i < 5), , . ( ). . , .
, :
static void Main(string[] args)
{
int[] numbers = { 4, 7, 1, 23, 43 };
int s = 0;
for (int i = 0; i < numbers.Length; i++)
{
s += numbers[i];
}
Console.WriteLine(s);
Console.ReadKey();
}
for, :
for (int i = 5; i > 0; i--) // 5
{
Console.WriteLine(i);
}
. , ( 50):
for (int i = 0; i <= 50; i+=2) // 26
{
Console.WriteLine(i);
}
while
while , , . , . :
while ( )
{
// ,
}
, .
, 0, 1, 2, 3, 4:
int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}
, :
while (true)
{
Console.WriteLine(" ");
}
do-while
while, , . .
do
{
// ,
}
while ( );
, , 5:
static void Main(string[] args)
{
int number;
do
{
Console.WriteLine(" 5");
number = Convert.ToInt32(Console.ReadLine());
}
while (number!= 5);
}
break
, break. , .
|
|
, , 13-. , , break:
static void Main(string[] args)
{
int[] numbers = { 4, 7, 13, 20, 33, 23, 54 };
bool b = false;
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] % 13 == 0)
{
b = true;
break;
}
}
Console.WriteLine(b? " 13": " 13");
Console.ReadKey();
}
continue
, .
, :
static void Main(string[] args)
{
int[] numbers = { 4, 7, 13, 20, 33, 23, 54 };
int s = 0;
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] % 2 == 0)
continue; //
s += numbers[i];
}
Console.WriteLine(s);
Console.ReadKey();
}
foreach - . , List . - , . foreach :
foreach ([] [] in [])
{
//
}
, foreach:
static void Main(string[] args)
{
int[] numbers = { 4, 7, 13, 20, 33, 23, 54 };
int s = 0;
foreach (int el in numbers)
{
s += el;
}
Console.WriteLine(s);
Console.ReadKey();
}
el . el 4, - 7 ..
, foreach break continue.
() . , () Collection was modified; enumeration operation may not execute.