♦ Initializing an Array in C#
♣ Example2: Initializing an Array in C#
Initializing Array in C# means that we are assigning values to the variables.There are various ways of Initializing an Array in C#.
In this example, Arrays are initialized with values.
Example
Input:
xxxxxxxxxx
1
using System;
2
3
4
class Program {
5
static void Main() {
6
7
// 1.
8
// can store 5 numbers
9
int[] numbers={0,1,4,9,16};
10
//below gives type of array
11
Console.WriteLine(numbers);
12
Console.WriteLine("\n");
13
14
15
// 2.
16
// directly giving input at the time initialization
17
string[] fruits={"apple","banana","cherry","dragonFruit","elderberry"};
18
for(int fruit=0; fruit<5; fruit++){
19
Console.WriteLine(fruits[fruit]);
20
}
21
22
23
24
25
26
27
28
}
29
}
30
Output:
1
1