How do you declare and initialize a fixed-size array in C#?
Short answer
Arrays in C# have a fixed size set at creation time, declared with square brackets after the element type, and can be initialized with a collection expression or explicit 'new' syntax.
Advertisement
AModel answer
Arrays in C# have a fixed size set at creation time, declared with square brackets after the element type, and can be initialized with a collection expression or explicit 'new' syntax.
int[] numbers = new int[5];
int[] scores = { 90, 85, 78 };
string[] names = new string[] { "Sam", "Alex" };Real-world example
Storing a fixed set of monthly sales figures where the count (12 months) never changes.
Likely follow-ups
- What's the default value of each element in a newly created int[] array?
Related topics