Sunday, 18 August 2013

Visual Studio .NET 2012

This tutorial will show you how to access elements array elements in C#.

Arrays in C# can accessed quite similar to how array elements are accessed in C and C++.

Example: Creating an array called numbers and assigning 5 to the fifth element of the array

Single-Dimensional Array
int[] numbers = new int[10];
numbers[4] = 5;

Multi-Dimensional Array
Declaring a multidimensional array and assigning 5 to the member located at [1, 1]:

int[,] numbers = new int[10][10];
numbers[1, 1] = 5;

Array of Arrays (Jagged Array)
The first element is an array of two integers, and the second is an array of three integers:

int[][] numbers = new int[][] { new int[] {1, 2}, new int[] {3, 4, 5}};

The following statements assign 11 to the first element of the first array and 22 to the second element of the second array:

numbers[0][0] = 11;
numbers[1][1] = 22;

Arrays are Objects

In C#, arrays are actually objects. System.Array is the abstract base type of all array types. You can use the properties, and other class members, that System.Array has.

Example: Finding length of the array

int[] numbers = {1, 2, 3, 4, 5};
int LengthOfNumbers = numbers.Length;

The System.Array class also provides many other useful methods/properties, to sort, search and copy arrays etc.

See Also
Using foreach with Arrays - DOTNET

Arrays Tutorial - DOTNET

Get Ebooks delivered to your email id


Subscribe to our channel


Powered by Blogger.

Home | Contact Us | DMCA | Terms of Service | Privacy | Advertise

Maven Scientists