In C # , As is the case with Java, there are two ways to create arrays. Only one array is said here.
1 ways to create:
Type [] array name ={ element 1, element 2,...};
For example:
// declare an array of integers
int[] array_01={1,2,3,4,5,6};
// outputs the contents of an array element
for(inti=0; i<array_01.length;i++)
{
Console.WriteLine(Array_01[i]);
}
Section 2 ways to create:
Type [] array name =new type [ number of elements ];
// initialize an array
int[] array_02=New int[6];
array_02[0]=1;
array_02[1]=2;
array_02[2]=3;
array_02[3]=4;
array_02[4]=5;
array_02[5]=6;
for(inti=0; i<array_02.length;i++)
{
Console.WriteLine(Array_02[i]);
}
The Foreach loop is primarily used to output the element values of an array.
foreach ( type variable in array name )
{
Statement
}
For example:
foreach(intIincharray_02)
{
Console.WriteLine(i);
}
This article is from the "Smile" blog, please be sure to keep this source http://yiyiweixiao.blog.51cto.com/2476874/1970510
18. Use of arrays and foreach loops inside C #