C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C# Array classC# provides an Array class to deal with array related operations. It provides methods for creating, manipulating, searching, and sorting elements of an array. This class works as the base class for all arrays in the .NET programming environment. C# Array class Signature[SerializableAttribute] [ComVisibleAttribute(true)] public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable Note: In C#, Array is not part of collection but considered as collection because it is based on the IList interface.C# Array Properties
C# Array Methods
C# Array Example
using System;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
// Creating an array
int[] arr = new int[6] { 5, 8, 9, 25, 0, 7 };
// Creating an empty array
int[] arr2 = new int[6];
// Displaying length of array
Console.WriteLine("length of first array: "+arr.Length);
// Sorting array
Array.Sort(arr);
Console.Write("First array elements: ");
// Displaying sorted array
PrintArray(arr);
// Finding index of an array element
Console.WriteLine("\nIndex position of 25 is "+Array.IndexOf(arr,25));
// Coping first array to empty array
Array.Copy(arr, arr2, arr.Length);
Console.Write("Second array elements: ");
// Displaying second array
PrintArray(arr2);
Array.Reverse(arr);
Console.Write("\nFirst Array elements in reverse order: ");
PrintArray(arr);
}
// User defined method for iterating array elements
static void PrintArray(int[] arr)
{
foreach (Object elem in arr)
{
Console.Write(elem+" ");
}
}
}
}
Output: length of first array: 6 First array elements: 0 5 7 8 9 25 Index position of 25 is 5 Second array elements: 0 5 7 8 9 25 First Array elements in reverse order: 25 9 8 7 5 0
Next TopicC# Command Line Arguments
|