Topic : |System.Array| Arrays Class, its Properties and Methods in C# Programming Language:



Array Class:

Arrays are one of the fundamental data structures in programming languages, providing a storage area for sequential data values. System.Array is the base class for all arrays in the common language runtime. It has methods for creating, manipulating, searching, and sorting the arrays we have talked about so far. System.Array implements several interfaces, like ICloneable, IList, ICollection, and IEnumerable. It also has many properties and methods to aid you in determining information about your array.



Properties of the Array Class:

The Following Table Describes some of the Most Commonly used Properties of the Array Class:


PropertyDescription:
IsFixedSizeGets a Value indicating whether the Array has a Fixed size.
IsReadOnlyGets a Value indicating Whether the Array is read Only.
IsSynchronizedGets a value indicating whether access to the Array is Synchronized - thread safe.
LengthGets a 32-bit integer that represents the total number of elements in all the dimensions of the array.
LongLengthGets a 64-bit Integer that represents the total number of Elements in all the Dimensions of the Array.
RankGets the Rank - Number of Dimensions - of the Array.
SyncRootGets an Object that can be used to Synchronize access to the Array.


Methods Of the Array Class:

The Following Table Describes some of the most commonly used Methods of the Array Class:


MethodsDescriptions:
ClearSets a range of elements in the Array to zero, to false, or to null, depending on the element type.
CloneCreates a Shallow copy of the Array:
ConvertAll<Tinput, TOutput>Converts An Array of one type to an array of another type.
Copy(Array, Array, int32)Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer.
Copy(Array, int32, Array, int32, int32)Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. The length and the indexes are specified as 32-bit integers.
CopyTo(Array, int32)Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. The index is specified as a 32-bit integer.
Createinstance(Type, int32)Creates a one-dimensional Array of the specified Type and length, with zero-based indexing.
Createinstance(Type, int32[])Creates a multidimensional Array of the specified Type and dimension lengths, with zero-based indexing. The dimension lengths are specified in an array of 32-bit integers.
Exists<T>Determines whether the specified array contains elements that match the conditions defined by the specified predicate.
Find<T>Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Array.
GetLengthGets a 32-bit integer that represents the number of elements in the specified dimension of the Array.
GetLongLengthGets a 64-bit integer that represents the number of elements in the specified dimension of the Array.
IndexOf(Array, Object)Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional Array.
Resize<T>Changes the number of elements of an array to the specified new size.
Reverse(Array)Reverses the sequence of the elements in the entire one-dimensional Array.
Sort(Array)Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.
ToStringReturns a string that represents the current object. (Inherited from Object.)
TrueForAll<T>Determines whether every element in the array matches the conditions defined by the specified predicate.
LastIndexOf(Array, Object)Searches for the specified object and returns the index of the last occurrence within the entire one-dimensional Array.
ForEach<T>Performs the specified action on each element of the specified array.


Example:

Let's have an example here, where we will see how to use Array Class. and we will use some methods of the Array Class.



/*Example: Array Class- InfoBrother:*/

using System;

namespace ArrayClass
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] list = {10, 30, 40, 20, 50, 80, 70, 60, 90};
            int[] temp = list;

            Console.Write("Original Array: ");

            foreach (int i in list)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            // reverse the array
            Array.Reverse(temp);
            Console.Write("Reversed Array: ");

            foreach (int i in temp)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            //sort the array
            Array.Sort(list);
            Console.Write("Sorted Array: ");

            foreach (int i in list)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            //Get Length of Array:
            int len = list.Length;
            Console.WriteLine("Length: The list has {0} Elements. ", len);

            //Array is Fixed or No?
            bool fix = list.IsFixedSize;
            Console.WriteLine("Array has Fixed Size?  {0} ", fix);

            //Get the Value from specific position:
            Console.WriteLine("Get Value from Index [3]: " + list.GetValue(3));
            Console.ReadKey();
        }
    }
}
















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement