using System;
class Program
{
static void Main(string[] args)
{
int[] testarray = new int[10];
for (int i = 1; i < =10; i++)
{
Console.WriteLine("Enter array element : {0}", i);
testarray[i] = int.Parse(Console.ReadLine());
}
foreach (int i in testarray)
{
Console.WriteLine(i);
}
}
}
Note: If you do not initialize an array at the time of declaration, the array members are automatically initialized to the default initial value for the array type. Also, if you declare the array as a field of a type, it will be set to the default value null when you instantiate the type.
Something about foreach
In a for loop, you should know the number of element of the array. If you don't, the C# language allows you to let the compiler use its internal mechanism to get this count and use it to know where to stop counting. To assist you with this, C# provides the foreach operator.
foreach loop is used on enumerator type only.
foreach is read-only loop.
No comments:
Post a Comment