Array.GetValue() Method in C# with Examples | Set – 3
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:
- Array.GetValue(Int32, Int32)
- Array.GetValue(Int64, Int64)
- Array.GetValue(Int32)
- Array.GetValue(Int64)
- Array.GetValue(Int32, Int32, Int32)
- Array.GetValue(Int64, Int64, Int64)
- Array.GetValue(Int32[])
- Array.GetValue(Int64[])
In this article we are explaining Array.GetValue(Int32, Int32, Int32) and Array.GetValue(Int64, Int64, Int64) method.
Array.GetValue(Int32, Int32, Int32) method is used to get the value at the specified position in the three-dimensional Array. The indexes are specified as 32-bit integers.
Syntax:
public object GetValue (int index1, int index2, int index3);
Here,
- “index1” is the number of array in which the element is located.
- “index2” is the index of the row in which the element is located.
- “index3” is the index of the column in which the element is located.
Returns: This method returns the element at the specified index define by the passed parameters in the 3D array. The value is a 32-bit integer value.
Exceptions:
- ArgumentException: The current Array does not have exactly Three dimensions.
- IndexOutOfRangeException: If index1 or index2 or index3 is outside the range of valid indexes for the corresponding dimension of the current Array.
Array.GetValue(Int64, Int64, Int64) method is used to get the value at the specified position in the three-dimensional Array. The indexes are specified as 64-bit integers.
Syntax:
public object GetValue (long index1, long index2, long index3);
Here,
- “index1” is the number of array in which the element is located.
- “index2” is the index of the row in which the element is located.
- “index3” is the index of the column in which the element is located.
Returns: This method returns the element at the specified index define by the passed parameters in the 3D array. The value is a 64-bit integer value.
Exceptions:
- ArgumentException: The current Array does not have exactly three dimensions.
- IndexOutOfRangeException: If index1 or index2 or index2 is outside the range of valid indexes for the corresponding dimension of the current Array.
Example 1:
C#
// C# program to demonstrate // Array.GetValue(Int32, Int32, Int32) // and array.GetValue(Int64, Int64, Int64) // method using System; public class GFG { public static void Main() { // declare a character // array of size 2x2x3 // 2 row and 3 column // number of array is 2 char [, , ] arr = new char [2, 2, 3]{ { // array 1 { 'A' , 'B' , 'C' }, { 'D' , 'E' , 'F' } }, { // array 2 { 'G' , 'H' , 'I' }, { 'J' , 'K' , 'L' }} }; // using GetValue(Int32, Int32, Int32) and // GetValue(Int64, Int64, Int64) method for ( int i = 0; i < 2; i++) { for ( int j = 0; j < 2; j++) { for ( int k = 0; k < 3; k++) { Console.WriteLine( "element at index [{0}, {1}, {2}] is : {3}" , i, j, k, arr.GetValue(i, j, k)); } } } } } |
element at index [0, 0, 0] is : A element at index [0, 0, 1] is : B element at index [0, 0, 2] is : C element at index [0, 1, 0] is : D element at index [0, 1, 1] is : E element at index [0, 1, 2] is : F element at index [1, 0, 0] is : G element at index [1, 0, 1] is : H element at index [1, 0, 2] is : I element at index [1, 1, 0] is : J element at index [1, 1, 1] is : K element at index [1, 1, 2] is : L
Example 2:
C#
// C# program to demonstrate // Array.GetValue(Int32, Int32, Int32) // and array.GetValue(Int64, Int64, Int64) // method using System; public class GFG { public static void Main() { // declare a character // array of size 2x2x3 // 2 row and 3 column // number of array is 2 string [,, ] arr = new string [2, 2, 3]; // use "SetValue()" method to set // the value at specified index arr.SetValue( "C++" , 0, 0, 0); arr.SetValue( "Java" , 0, 0, 1); arr.SetValue( "C#" , 0, 0, 2); arr.SetValue( "Perl" , 0, 1, 0); arr.SetValue( "Python" , 0, 1, 1); arr.SetValue( "PHP" , 0, 1, 2); arr.SetValue( "GO" , 1, 0, 0); arr.SetValue( "Ruby" , 1, 0, 1); arr.SetValue( "F#" , 1, 0, 2); arr.SetValue( "JavaScript" , 1, 1, 0); arr.SetValue( "SQL" , 1, 1, 1); arr.SetValue( "kotlin" , 1, 1, 2); // using GetValue(Int32, Int32, Int32) and // GetValue(Int64, Int64, Int64) method for ( int i = 0; i < 2; i++) { for ( int j = 0; j < 2; j++) { for ( int k = 0; k < 3; k++) { Console.WriteLine( "element at index [{0}, {1}, {2}] is : {3}" , i, j, k, arr.GetValue(i, j, k)); } } } } } |
element at index [0, 0, 0] is : C++ element at index [0, 0, 1] is : Java element at index [0, 0, 2] is : C# element at index [0, 1, 0] is : Perl element at index [0, 1, 1] is : Python element at index [0, 1, 2] is : PHP element at index [1, 0, 0] is : GO element at index [1, 0, 1] is : Ruby element at index [1, 0, 2] is : F# element at index [1, 1, 0] is : JavaScript element at index [1, 1, 1] is : SQL element at index [1, 1, 2] is : kotlin
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.
Please Login to comment...