Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Wednesday, April 25, 2007

foreach statement

C# introduce the foreach statement, which iterates through the elements of an entire array or collection.

foreach ( type identifier in arrayName )
statement



simple declaration and output syntax

Declaring and Creating Arrays

int[] c = new int[ 12 ];

other ways:
int[] c; // declare the array variable
c = new int[ 12 ]; // create the array; assign to array variable

single lines for some variables:
string[] b = new string[ 100 ], x=newstring[ 27 ];




it's suggested that :

For readability, declare only one variable per declaration. Keep each declaration on a separate line and include a comment describing the variable being declared.

common output for printing array elements:
- using for loop
// output each array element's value \
for ( int counter = 0; counter <>
Console.WriteLine( "{0,5}{1,8}", counter, array[ counter ] );

output: