0% found this document useful (0 votes)
18 views3 pages

Array Class Tp2a by MR Sonwabiso (2024)

Uploaded by

Sonwabiso tedee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Array Class Tp2a by MR Sonwabiso (2024)

Uploaded by

Sonwabiso tedee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ARRAY CLASS

The Array Class in C# is not a part of the System.Collections namespace. It is a part of


the System namespace. But still, we considered it as a collection because it Implement
the IList interface. The Array class provides the following methods and properties:

1. Sort(<array>): Sorting the array elements

2. Reverse (<array>): Reversing the array elements

3. Copy (src, dest, n): Copying some of the elements or all elements from the old
array to the new array

4. GetLength(int): A 32-bit integer that represents the number of elements in the


specified dimension.

5. Length: It Returns the total number of elements in all the dimensions of the Array;
zero if there are no elements in the array.

Example to Understand Array Class Methods and Properties in C#


Let us see an example for understanding the Method and Properties of the Array class
in C#. The following example is self-explained, so please go through the comment
lines.
using System;

namespace ArayDemo

class Program

static void Main(string[] args)

//Creating and Initializing an Array of Integers

//Size of the Array is 10

int[] Numbers = { 17, 23, 4, 59, 27, 36, 96, 9, 1, 3 };

//Printing the Array Elements using a for Loop


Console.WriteLine("Original Array Elements :");

for (int i = 0; i < Numbers.Length; i++)

Console.Write(Numbers[i] + " ");

Console.WriteLine();

//Sorting the Array Elements by using the Sort method of Array Class

Array.Sort(Numbers);

//Printing the Array Elements After Sorting using a foreach loop

Console.WriteLine("\nArray Elements After Sorting :");

foreach (int i in Numbers)

Console.Write(i + " ");

Console.WriteLine();

//Reversing the array elements by using the Reverse method of Array Class

Array.Reverse(Numbers);

//Printing the Array Elements in Reverse Order

Console.WriteLine("\nArray Elements in the Reverse Order :");

foreach (int i in Numbers)

Console.Write(i + " ");

Console.WriteLine();
//Creating a New Array

int[] NewNumbers = new int[10];

//Copying Some of the Elements from Old array to new array

//We declare the array with size 10 and we copy only 5 elements.

//So the rest 5 elements will take the default value. In this array, it will take 0

Array.Copy(Numbers, NewNumbers, 5);

//Printing the Array Elements using for Each Loop

Console.WriteLine("\nNew Array Elements :");

foreach (int i in NewNumbers)

Console.Write(i + " ");

Console.WriteLine();

Console.WriteLine("\nNew Array Length using Length Property


:{0}",newNumbers.Length);

Console.WriteLine("New Array Length using GetLength Method :{1)}",


NewNumbers.GetLength(0));

Console.ReadKey();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy