What are the various ways to define arrays in C#?

In C#, there are several ways to define an array:

  1. Initialize an array directly by listing the array elements within braces.
int[] numbers = { 1, 2, 3, 4, 5 };
  1. Initialize an array using the new keyword: Initialize an array by specifying the array size with the new keyword.
int[] numbers = new int[5];
  1. 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[];
Leave a Reply 0

Your email address will not be published. Required fields are marked *