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

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.