Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are the interfaces implemented by Array class in C#?
The System.Array class in C# implements several important interfaces that provide essential functionality for array operations. These interfaces include ICloneable, IList, ICollection, and IEnumerable, each serving specific purposes in array manipulation and iteration.
Understanding these interfaces helps you leverage the full capabilities of arrays in C# and work with them more effectively in different scenarios.
Interfaces Implemented by Array Class
| Interface | Purpose | Key Methods/Properties |
|---|---|---|
| ICloneable | Creates a shallow copy of the array | Clone() |
| IList | Provides indexed access and modification | this[index], Add(), Remove() |
| ICollection | Provides count and synchronization support | Count, IsSynchronized, SyncRoot |
| IEnumerable | Enables foreach iteration | GetEnumerator() |
Using ICloneable Interface
The ICloneable interface provides the Clone() method to create a copy of an object. When implemented in custom classes, it allows creating duplicates of objects −
Example
using System;
class Car : ICloneable {
int width;
public Car(int width) {
this.width = width;
}
public object Clone() {
return new Car(this.width);
}
public override string ToString() {
return string.Format("Width of car = {0}", this.width);
}
}
class Program {
static void Main() {
Car carOne = new Car(1695);
Car carTwo = carOne.Clone() as Car;
Console.WriteLine("{0}mm", carOne);
Console.WriteLine("{0}mm", carTwo);
}
}
The output of the above code is −
Width of car = 1695mm Width of car = 1695mm
Using Array.Clone() Method
Since arrays implement ICloneable, you can use the Clone() method to create a shallow copy of an array −
Example
using System;
class Program {
static void Main() {
string[] arr = { "one", "two", "three", "four", "five" };
string[] arrCloned = arr.Clone() as string[];
Console.WriteLine("Original array: " + string.Join(",", arr));
Console.WriteLine("Cloned array: " + string.Join(",", arrCloned));
// Modify original array to show they are independent
arr[0] = "modified";
Console.WriteLine("After modifying original:");
Console.WriteLine("Original array: " + string.Join(",", arr));
Console.WriteLine("Cloned array: " + string.Join(",", arrCloned));
}
}
The output of the above code is −
Original array: one,two,three,four,five Cloned array: one,two,three,four,five After modifying original: Original array: modified,two,three,four,five Cloned array: one,two,three,four,five
Using IEnumerable Interface
Arrays implement IEnumerable, which enables iteration using foreach loops and LINQ operations −
Example
using System;
using System.Linq;
class Program {
static void Main() {
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Using foreach (IEnumerable)
Console.WriteLine("Even numbers:");
foreach (int num in numbers) {
if (num % 2 == 0) {
Console.Write(num + " ");
}
}
Console.WriteLine();
// Using LINQ (also uses IEnumerable)
var evenNumbers = numbers.Where(x => x % 2 == 0);
Console.WriteLine("Even numbers using LINQ: " + string.Join(", ", evenNumbers));
}
}
The output of the above code is −
Even numbers: 2 4 6 8 10 Even numbers using LINQ: 2, 4, 6, 8, 10
Conclusion
The System.Array class implements multiple interfaces that provide essential functionality like cloning, iteration, and collection operations. The ICloneable interface enables creating shallow copies, while IEnumerable allows foreach loops and LINQ operations, making arrays versatile and powerful data structures in C#.
