C# Initialize an Integer Array
To initialize an integer Array in C#, declare a variable of type int[] and assign the comma separated values enclosed in flower braces to the array variable.
Example
In the following example, we initialize an integer array in a variable arr
, and print the contents of this array to console.
Program.cs
</>
Copy
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
int[] arr = {2, 4, 6, 8};
Console.WriteLine("Integer Array");
for (int i=0; i < arr.Length; i++) {
Console.WriteLine(arr[i]);
}
}
}
}
Output
Integer Array
2
4
6
8
Conclusion
In this C# Tutorial, we learned how to initialize an integer array, with examples.