What are the various ways to define arrays in C#?
In C#, there are several ways to define an array:
- Initialize an array directly by listing the array elements within braces.
int[] numbers = { 1, 2, 3, 4, 5 };
- Initialize an array using the new keyword: Initialize an array by specifying the array size with the new keyword.
int[] numbers = new int[5];
- Initialize an array using static methods of the Array class: create and initialize an array using static methods of the Array class.
int[] numbers = Array.CreateInstance(typeof(int), 5) as int[];