Home » .Net

Explain Array Class in C#

Learn: What is an array class in C#, what are its methods, properties? This post contains example on Array class with its methods and properties.

Array is a class in C#.Net, which is declared/defined in System namespace. Array class has many inbuilt methods and properties, which makes array manipulation (array related operations) very easy.

Some of them are, which are commonly used:

  • Sort(array);
  • Reverse(array);
  • Copy(Source,dest,n);
  • GetLength(index)
  • Length

Consider the program, which are using all these methods/properties:

using System;

class SDARRAY2
{
	static void Main()
	{
		int [] arr = {80,20,30,12,89,34,1,3,0,8};

		for(int i=0;i<arr.Length;i++)
			Console.Write(arr[i]+" ");
		
		Console.WriteLine();

		Array.Sort(arr);
		foreach(int i in arr)
			Console.Write(i+" ");
		
		Console.WriteLine();

		Array.Reverse(arr);
		foreach(int i in arr)
			Console.Write(i+" ");
			
		Console.WriteLine();

		int []brr = new int [10];

		Array.Copy(arr,brr,5);
		foreach(int i in brr)
			Console.Write(i+" ");
	}
}

Output

80 20 30 12 89 34 1 3 0 8
0 1 3 8 12 20 30 34 80 89
89 80 34 30 20 12 8 3 1 0
89 80 34 30 20 0 0 0 0 0
Press any key to continue . . .

Above example demonstrate of use of Array class methods. Sort() method perform sorting, Reverse() method reverse the elements of array and Copy method is used to copy one array to another array.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.