To create an array of value types in C#, follow these steps:
int[] myArray;
myArray = new int[10];
Alternatively, you can combine step 1 and step 2 into a single statement:
int[] myArray = new int[10];
for (int i = 0; i < myArray.Length; i++)
{
myArray[i] = i + 1;
}
This loop sets the value of each element in the array to its index plus one. The resulting array will contain the values 1 through 10.
Alternatively, you can initialize an array using an array initializer syntax. For example:
int[] myArray = { 1, 2, 3, 4, 5 };
This initializes the array with the values 1 through 5.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-09-07 11:00:00 +0000
Seen: 9 times
Last updated: May 24 '22