Array Operations and Methods in c#
Array data types in C# programming are versatile data types that store a continuous sequence of data. They offer a variety of built-in programming methods that allow C# users to perform various array operations on existing array data types, such as array index sorting, data element searching, copying from one array to another, and modifying array elements. All of these array programming operations are provided by the Array class, which is a built-in feature of the System namespace library in C#.

So, let’s take a closer look at some of the common array operations and methods provided by the Array class in C# programming.
Common Array Operations in C#.
Accessing and Modifying Array Elements.
C# users can access and manage array elements in an array program using the array storage index location, and can access and manually modify the desired array element as needed.
int[] integers = { 9, 7, 1, 3, 5, 7, 6 };
Console.WriteLine(integers[1]); // This accesses the second element in the array. Result – 7
// Modifying a particular array element.
integers[3] = 33;
Console.WriteLine(integers[3]); // Result – 33
Finding the length of an array.
In a user-defined array program, each array has a specific length property, which represents the number of array elements in the current array data type.
int[] integers = { 9, 7, 1, 3, 5, 7, 6 };
Console.WriteLine(integers.Length); // Result – 7
Looping concept in arrays.
C# users use existing loops to loop or iterate over array elements in a particular sequence or number of rows. For example, they can apply for, foreach, etc.
Looping concept example in arrays.
using System;
class Program
{
static void Main()
{
int[] integers = { 9, 7, 1, 3, 5, 7, 6 };
// Using the for loop in a C# array program
for (int p = 0; p <=integers.Length; p++)
{
Console.WriteLine(integers[p]);
}
// Using the foreach loop in a C# array program
foreach (int intgrs in integers)
{
Console.WriteLine(intgrs);
}
}
}
Array Methods Concept from the Array Class.
The Array class in C# programs provides several built-in methods and functions for performing multiple individual programming operations on arrays. These include:
Array.Sort() function/method.
The Array.Sort() method in a C# array program sorts and arranges the numeric value data elements stored in an array in ascending order. If needed, it can also sort the stored array elements in descending order using a custom comparer.
Array.Sort() method example in C#.
using System;
class Program
{
static void Main()
{
int[] integers = { 9, 7, 1, 3, 5, 7, 6 };
// Sorting array data in ascending order
Array.Sort(integers);
// Result – 1, 3, 5, 6, 7, 7, 9
foreach (int intgrs in integers)
{
Console.WriteLine(intgrs);
}
}
}
Array.Reverse() function/method.
In a C# array program, the Array.Reverse() method reverses the order of data elements in the current array and prints them to the console screen.
Array.Reverse() method example in C#.
using System;
class Program
{
static void Main()
{
int[] integers = { 9, 7, 1, 3, 5, 7, 6 };
// Reverse the current array elements
Array.Reverse(integers);
// Result – 6, 7, 5, 3, 1, 7, 9
foreach (int intgrs in integers)
{
Console.WriteLine(intgrs);
}
}
}
Array.Copy() function/method.
In a C# array program, the Array.Copy() method directly copies array data elements from a complete, user-defined array to another array.
Array.Copy() method example in C#.
using System;
class Program
{
static void Main()
{
int[] start = { 7, 8, 1, 9, 3 };
int[] end = new int[4];
//This copies the first 3 elements
// from the start array to the end array.
Array.Copy(start, end, 4);
// Result – 7, 8, 1, 9
foreach(int value in end)
{
Console.WriteLine(value);
}
}
}
Array.Clone() function/method.
In a C# array program, the Array.Clone() method creates a shallow copy of an array. This can be useful for creating a new array with the same values as the original array in the current program.
Example of the Array.Clone() method in C#.
using System;
class Program
{
static void Main()
{
int[] real = { 77, 89, 12, 54, 67 };
int[] copy = (int[])real.Clone();
// Changing the copy here will not affect the original array.
copy[3] = 22;
Console.WriteLine(“\n List of Original array:”);
foreach (int num in real)
{
Console.WriteLine(num); // Result is – 77, 89, 12, 54, 67
}
Console.WriteLine(“\n Element of Cloned array:”);
foreach (int num in copy)
{
Console.WriteLine(num); // Result is – 77, 89, 12, 22, 67
}
}
}
Array.Clear() function/method.
In a C# array program, the Array.Clear() method displays all elements of a user-defined array by setting them to their corresponding values (0 for numeric value types, false for bool, null for reference data types).
Example of the Array.Clear() method in C#.
using System;
class Program
{
static void Main()
{
int[] integers = { 9, 7, 1, 3, 5, 7, 6 };
// This clears all elements in the array
Array.Clear(integers, 0, integers.Length);
// Result is – 0, 0, 0, 0, 0, 0, 0
foreach (int intgrs in integers)
{
Console.WriteLine(intgrs);
}
}
}
Array.IndexOf() function/method.
The Array.IndexOf() method in a C# array program finds the index at which a specified array element appears first.
Example of the Array.IndexOf() method in C#.
using System;
class Program
{
static void Main()
{
int[] integers = { 9, 7, 1, 3, 5, 7, 6 };
// here it finds the index of the value 5
int index = Array.IndexOf(integers, 5);
Console.WriteLine(“Index of 5 is – ” + index); // Result is – 4
}
}
Array.LastIndexOf() function/method.
In a C# array program, the Array.LastIndexOf() method finds the last instance of a specified array element at an index in a user-defined array.
Example of the Array.LastIndexOf() method in C#.
using System;
class Program
{
static void Main()
{
int[] integers = { 9, 7, 1, 3, 5, 7, 6 };
// Here it finds the last index of the value 3
int lastIndex = Array.LastIndexOf(integers, 3);
Console.WriteLine(“Here the last index of 3 is – ” + lastIndex); // Result is – 3
}
}
Array.Exists() function/method.
In a C# array program, the Array.Exists() method checks whether an element in the array satisfies a condition based on a predicate (lambda function).
Example of the Array.Exists() method in C#.
using System;
class Program
{
static void Main()
{
int[] integers = { 9, 7, 1, 3, 5, 7, 6 };
// Here it checks whether a given number is greater than 8 in the current array.
bool exists = Array.Exists(integers, intgrs => intgrs > 8);
Console.WriteLine(“Is there any number greater than 8? – ” + exists); // Result is – True
}
}
Array.Find() function/method.
The Array.Find() method in a C# array program displays the first element in the current array program that satisfies the given condition.
Example of the Array.Find() method in C#.
using System;
class Program
{
static void Main()
{
int[] integers = { 9, 7, 1, 3, 5, 7, 6 };
// Here we are finding the first number greater than 7
int search = Array.Find(integers, intgrs => intgrs > 7);
Console.WriteLine(“Here is the number greater than 7 – ” + search); // Result is – 9
}
}
Array.FindAll() function/method.
In a C# array program, the Array.FindAll() method returns all array elements that satisfy a particular array condition.
Example of the Array.FindAll() method in C#.
using System;
class program
{
static void Main()
{
int[] integers = { 9, 7, 1, 3, 5, 7, 6 };
// here it Find all numbers greater than 3 in the current array.
int[] searchintegers = Array.FindAll(integers, intgrs => intgrs > 3);
Console.WriteLine(“Display the Numbers is greater than 3 -“);
foreach (int integers in searchintegers)
{
Console.WriteLine(intgrs); // Result is – 9, 7, 5, 7, 6
}
}
}
Array.ForEach() function/method.
In a C# array program, the Array.ForEach() method applies an action to each element in the array.
Example of the Array.ForEach() method in C#.
using System;
class Program
{
static void Main()
{
int[] integers = { 9, 7, 1, 3, 5, 7, 6 };
// here it prints each number in the array
Array.ForEach(integers, intgrs => Console.WriteLine(intgrs)); // Result is – 9 7 1 3 5 7 6
}
}
Array.Resize() function/method.
In a C# array program, the Array.Resize() method is used to modify the size of an array. It creates a new array with a user-defined array size.
Example of the Array.Resize() method in C#.
using System;
class Program
{
static void Main()
{
int[] integers = { 9, 7, 1, 3, 5, 7, 6 };
// Here it resizes the array to 9 elements
Array.Resize(ref integers, 9);
integers[7] = 10;
integers[8] = 11;
// Result is – 9, 7, 1, 3, 5, 7, 6, 10, 11
foreach (int intgrs in integers)
{
Console.WriteLine(intgrs);
}
}
}
Array Operations and Methods Summary in C#.
- Array.Sort() – This function method sorts the array in ascending order in a C# array program.
- Array.Reverse() – This function method reverses the order of elements in a C# array program.
- Array.Copy() – This function method copies elements from one array to another in a C# array program.
- Array.Clone() – This method creates a shallow copy of an array in a C# array program.
- Array.Clear() – This method clears the elements of an array in a C# array program, setting them to their default values.
- Array.IndexOf() – This method finds the index of the first occurrence of an element in an array in a C# array program.
- Array.LastIndexOf() – This method finds the index of the last occurrence of an element in an array in a C# array program.
- Array.Exists() – This method in a C# array program checks whether an array element satisfies a condition.
- Array.Find() – This method in a C# array program finds the first element that satisfies a condition.
- Array.FindAll() – This method in a C# array program finds all elements that satisfy a condition.
- Array.ForEach() – This method in a C# array program applies an action to each element of an array.
- Array.Resize() – This method in a C# array program modifies the array to a new size.
Arrays are a powerful structured data in C# programming, and they come with a variety of built-in array function methods to simplify data manipulation in index sequences. Whether you’re sorting an array, finding an array, or modifying the array’s size, the Array class provides all the functions and features you need to handle any type of common array operation.
