Home > Array > Array in Csharp C# – A Complete Tutorial

Array in Csharp C# – A Complete Tutorial

Array – A data structure that contains a number of variables called elements of the array

  • All of the array elements must be of the same type
  • Arrays are zero indexed
  • Arrays are objects

Arrays can be:

Single Dimensional Array – an array with the rank of one.

Multidimensional Array – an array with a rank greater than one

Jagged Array – an array whose elements are array.

System.Array – is an abstract class that provides many methods that you can use when working with arrays.

Example:

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

Clear – Sets a range of elements in the Array to zero, to false, or to null, depending on the element type.

Clone – Creates a shallow copy of the Array.

Find<T> – Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Array.

GetLength – Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.

Reverse – Reverses the sequence of the elements in the entire one-dimensional Array.

Sort – Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.

IndexOf – returns the index of the first occurrence of a value.

Length – Gets the number of elements in the specified dimension of the array.

GetLength – returns the elements of the given dimension.

Sample code:

public class SamplesArray  {

 

public static void Main()  {

 

// Creates and initializes a new integer array and a new Object array.

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

Object[] myObjArray = new Object[5] { 26, 27, 28, 29, 30 };

 

// Prints the initial values of both arrays.

Console.WriteLine( “Initially,” );

Console.Write( “integer array:” );

PrintValues( myIntArray );

Console.Write( “Object array: ” );

PrintValues( myObjArray );

 

// Copies the first two elements from the integer array to the Object array.

System.Array.Copy( myIntArray, myObjArray, 2 );

 

// Prints the values of the modified arrays.

Console.WriteLine( “\nAfter copying the first two elements of the integer array to the Object array,” );

Console.Write( “integer array:” );

PrintValues( myIntArray );

Console.Write( “Object array: ” );

PrintValues( myObjArray );

 

// Copies the last two elements from the Object array to the integer array.

System.Array.Copy( myObjArray, myObjArray.GetUpperBound(0) – 1, myIntArray, myIntArray.GetUpperBound(0) – 1, 2 );

 

// Prints the values of the modified arrays.

Console.WriteLine( “\nAfter copying the last two elements of the Object array to the integer array,” );

Console.Write( “integer array:” );

PrintValues( myIntArray );

Console.Write( “Object array: ” );

PrintValues( myObjArray );

}

 

public static void PrintValues( Object[] myArr )  {

foreach ( Object i in myArr )  {

Console.Write( “\t{0}”, i );

}

Console.WriteLine();

}

 

public static void PrintValues( int[] myArr )  {

foreach ( int i in myArr )  {

Console.Write( “\t{0}”, i );

}

Console.WriteLine();

}

}

/*

This code produces the following output.

Initially,

integer array:  1       2       3       4       5

Object array:   26      27      28      29      30

After copying the first two elements of the integer array to the Object array,

integer array:  1       2       3       4       5

Object array:   1       2       28      29      30

After copying the last two elements of the Object array to the integer array,

integer array:  1       2       3       29      30

Object array:   1       2       28      29      30

*/

 

Categories: Array Tags:

Post Comments Below

  1. ProDe
    April 19th, 2011 at 22:02 | #1

    Hi, super interesting. Thanks!

  1. No trackbacks yet.