Sunday, 18 August 2013


Visual Studio .NET 2012


This tutorial will help you how to declare Arrays in C#




Sample Code:


int[] numbers; // declaring numbers as an int array of any size

numbers = new int[10];  // numbers is a 10-element array

numbers = new int[20];  // now it's a 20-element array


In C# you can declare single-dimensional arrays, multidimensional arrays, and array-of-arrays (jagged arrays).


Example: Declaring different kinds of Arrays


Single-dimensional arrays


int[] numbers; // declaring numbers as an int array of any size



Multidimensional arrays:



string[,] names;


Array-of-arrays (jagged):


byte[][] scores;


Writing the above code, does not actually create the arrays. Note that memory must be allocated to the array.


Example: How to create Arrays


Single-dimensional arrays:


int[] numbers = new int[5]; //allocating memory to array numbers using new keyword. The length of the array is 5.


Multidimensional arrays:


string[,] names = new string[5,5]; //A 5x5 array names has been created. Row x Column. 5 rows and 5 columns.


Array-of-arrays (jagged):


byte[][] scores = new byte[5][];

for (int x = 0; x < scores.Length; x++)

{

   scores[x] = new byte[4];

}


To create a three-dimensional rectangular array:


int[,,] buttons = new int[4,5,3];


Example: Creating a single-dimensional array of three-dimensional array of two-dimensional arrays of type int.


int[][,,][,] numbers;



Code Example: Array

 
Output






Arrays Tutorial - DOTNET

Get Ebooks delivered to your email id

Comments

Subscribe to our channel

Facebook

Powered by Blogger.

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

Maven Scientists