Open In App

Array.GetValue() Method in C# with Examples | Set – 4

Improve
Improve
Like Article
Like
Save
Share
Report

Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows:

In this article we are explaining Array.GetValue(Int32[]) and Array.GetValue(Int64[]) method.

Array.GetValue(Int32[]) method is used to get the value at the specified position in the multidimensional-dimensional Array. The indexes are passed to this method by a 1D array, that means the 1D array which is passed that contains the specified index as array element of 1D array, and the element is to be searched that is at that index which is in the 1D array. The indexes are specified as 32-bit integers. 

Syntax: 
 

public object GetValue (int[] index);

Here “index” is a 1D array of 32-bit integers that represent the index specifying the position of the element to be searched. searched.

Returns: This method returns the element at the specified index define by the passed 1D array to the GetValue method. The value is a 32-bit integer value.

Exceptions:

  • ArgumentException: If the number of dimensions in the current array is not equal to the number of elements in index array which is passed to GetValue method.
  • IndexOutOfRangeException: If the elements in index array defines a range which is outside the range of valid indexes for the corresponding dimension of the current array .
  • ArgumentNullException: if the index array is null then it throws.

 

Array.GetValue(Int64[]) method is used to get the value at the specified position in the multidimensional-dimensional Array. The indexes are passed to this method by a 1D array, that means the 1D array which is passed that contains the specified index as array element of 1D array, and the element is to be searched that is at that index which is in the 1D array. The indexes are specified as 64-bit integers. 

Syntax: 

public object GetValue (long[] index);

Here, “index[]” is a 1D array of 64-bit integers that represent the index specifying the position of the element to be searched.

Returns: This method returns the element at the specified index define by the passed 1D array to the GetValue method. The value is a 32-bit integer value.

Exceptions:

  • ArgumentException: If the number of dimensions in the current array is not equal to the number of elements in index array which is passed to GetValue method.
  • ArgumentOutOfRangeException: If the elements in index array defines a range which is outside the range of valid indexes for the corresponding dimension of the current array .
  • ArgumentNullException: if the index array is null then it throws.

Example 1:
 

C#




// C# program to demonstrate the
// Array.GetValue(Int32[]) and
// Array.GetValue(Int64[]) method
using System;
public class GFG {
  
public static void Main() {
    
    // declare a character
    // array of size 1x2x2x3
    // it has 2 3D array
    // which have 2 row and 3 column each
    char[, , , ] arr = new char[1, 2, 2, 3]{
        {
  
            {// array 1
             {'A', 'B', 'C'},
             {'D', 'E', 'F'}
  
            },
            {// array 2
             {'G', 'H', 'I'},
             {'J', 'K', 'L'}}
  
        }
        /*Array look like
   |----------------------------------|
   | -------------------------------- |
   |  |A|          |B|           |C|  |
   |  |D|          |E|           |F|  |
   | -------------------------------- |
   | -------------------------------- |
   |  |G|          |H|           |I|  |
   |  |J|          |K|           |L|  |
   | -------------------------------- |
   |__________________________________|
   */
    };
  
    // initialize a integer array
    // contains the index of the element
    // index of 'L'
    int[] indx = new int[4]{0, 1, 1, 2};
  
    // "indx" array is passing
    // to GetValue(Int32[]) method
    Console.WriteLine("Element at index [0, 1, 1, 2] is : {0}",
                                           arr.GetValue(indx));
  }
}


Output

Element at index [0, 1, 1, 2] is : L

 

Example 2:

C#




// C# program to demonstrate the
// Array.GetValue(Int32[]) and
// Array.GetValue(Int64[]) method
using System;
  
class GFG {
  
public static void Main() {
    
    // declare a character
    // array of size 1x2x2x3
    // it has 2 3D array
    // which have 2 row and
    // 3 column each
    string[, , , ] arr = new string[1, 2, 2, 3];
    
    /*Array look like
|------------------------------------|
| ---------------------------------- |
|  |C++|          |Java|       |C#|  |
|  |Perl|          |python|    |PHP| |
| ---------------------------------- |
| ---------------------------------- |
|  |Ruby|        |F#|       |Julia|  |
|  |SQL|        |Kotlin|       |GO|  |
| ---------------------------------- |
|____________________________________|
*/
  
    arr.SetValue("C++", 0, 0, 0, 0);
    arr.SetValue("Java", 0, 0, 0, 1);
    arr.SetValue("C#", 0, 0, 0, 2);
    arr.SetValue("Perl", 0, 0, 1, 0);
    arr.SetValue("Python", 0, 0, 1, 1);
    arr.SetValue("PHP", 0, 0, 1, 2);
    arr.SetValue("Ruby", 0, 1, 0, 1);
    arr.SetValue("F#", 0, 1, 0, 1);
    arr.SetValue("Julia", 0, 1, 0, 2);
    arr.SetValue("SQL", 0, 1, 1, 0);
    arr.SetValue("kotlin", 0, 1, 1, 1);
    arr.SetValue("GO", 0, 1, 1, 2);
  
    // initialize a integer array
    // contains the index of the element
    // index of 'C#'
    int[] indx = new int[4]{0, 1, 1, 2};
  
    // "indx" array is passing
    // to GetValue(Int32[]) method
    Console.WriteLine("Element at index [0, 1, 1, 2] is : {0}",
                                           arr.GetValue(indx));
  }
}


Output

Element at index [0, 1, 1, 2] is : GO

Note: For online compiler it is not possible to use 32-bit or 64-bit integer. Use offline compiler for 32 or 64-bit integer.



Last Updated : 17 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads