background image

Single dimension array

<< Arrays | C# Console Application, >>
<< Arrays | C# Console Application, >>



Example 1.

We will create a C# Console Application that will accept the names of students in an
single dimension array and display it back.

int value = 0, cnt = 0;

//Accept how many students names to enter
Console.Write("Enter the number of students name to enter:
");
value = System.Int32.Parse(Console.ReadLine()) ;

string[] arrnames = new string [value];

for(cnt = 0; cnt<value;cnt++)
{
Console.Write("Enter the name of student " + (cnt + 1)
+ ":", "Student Name");
arrnames[cnt] = Console.ReadLine();
}

Console.WriteLine("Pulling Values from the Array");

//Display the entered value to the text box
for(cnt = 0; cnt < value; cnt++)
{
Console.WriteLine(arrnames[cnt]);
}
Above example will accept number of names to be entered and will add the names in a
loop and then redisplay it on the Console. Note that we have not written any error
handling code that is left to the reader as an exercise.
The Syntax for multi-dimension arrays is as follows:
Previously we saw how we can store multiple names of students. But, if we want to store
related data of students like first name, middle name, last name. In such situations you
can use multi dimension arrays, such as two-or-three dimension arrays.
Datatype [] ArrayName = new Datatype[number of 1
st
element,
number of 2
nd
element,....];